Skip to content

Commit

Permalink
refactor: add 'deno_os' crate (#27655)
Browse files Browse the repository at this point in the history
This commit creates "deno_os" extension crate and moves
numerous ops from "runtime/" crate to the new crate.
  • Loading branch information
bartlomieju authored Jan 14, 2025
1 parent c943f56 commit 974e2f4
Show file tree
Hide file tree
Showing 29 changed files with 145 additions and 80 deletions.
25 changes: 22 additions & 3 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ deno_kv = { version = "0.93.0", path = "./ext/kv" }
deno_napi = { version = "0.116.0", path = "./ext/napi" }
deno_net = { version = "0.177.0", path = "./ext/net" }
deno_node = { version = "0.123.0", path = "./ext/node" }
deno_os = { version = "0.1.0", path = "./ext/os" }
deno_telemetry = { version = "0.7.0", path = "./ext/telemetry" }
deno_tls = { version = "0.172.0", path = "./ext/tls" }
deno_url = { version = "0.185.0", path = "./ext/url" }
Expand Down
2 changes: 1 addition & 1 deletion cli/js/40_bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
restorePermissions,
} from "ext:cli/40_test_common.js";
import { Console } from "ext:deno_console/01_console.js";
import { setExitHandler } from "ext:runtime/30_os.js";
import { setExitHandler } from "ext:deno_os/30_os.js";
const {
op_register_bench,
op_bench_get_origin,
Expand Down
2 changes: 1 addition & 1 deletion cli/js/40_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const {
TypeError,
} = primordials;

import { setExitHandler } from "ext:runtime/30_os.js";
import { setExitHandler } from "ext:deno_os/30_os.js";

// Capture `Deno` global so that users deleting or mangling it, won't
// have impact on our sanitizers.
Expand Down
2 changes: 1 addition & 1 deletion cli/task_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ async fn listen_ctrl_c(kill_signal: KillSignal) {
#[cfg(unix)]
async fn listen_and_forward_all_signals(kill_signal: KillSignal) {
use deno_core::futures::FutureExt;
use deno_runtime::signal::SIGNAL_NUMS;
use deno_runtime::deno_os::signal::SIGNAL_NUMS;

// listen and forward every signal we support
let mut futures = Vec::with_capacity(SIGNAL_NUMS.len());
Expand Down
2 changes: 1 addition & 1 deletion cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ fn create_web_worker_callback(
/// Instead probe for the total memory on the system and use it instead
/// as a default.
pub fn create_isolate_create_params() -> Option<v8::CreateParams> {
let maybe_mem_info = deno_runtime::sys_info::mem_info();
let maybe_mem_info = deno_runtime::deno_os::sys_info::mem_info();
maybe_mem_info.map(|mem_info| {
v8::CreateParams::default()
.heap_limits_from_system_memory(mem_info.total, 0)
Expand Down
2 changes: 1 addition & 1 deletion ext/node/polyfills/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { validateIntegerRange } from "ext:deno_node/_utils.ts";
import process from "node:process";
import { isWindows } from "ext:deno_node/_util/os.ts";
import { os } from "ext:deno_node/internal_binding/constants.ts";
import { osUptime } from "ext:runtime/30_os.js";
import { osUptime } from "ext:deno_os/30_os.js";
import { Buffer } from "ext:deno_node/internal/buffer.mjs";
import { primordials } from "ext:core/mod.js";
const { StringPrototypeEndsWith, StringPrototypeSlice } = primordials;
Expand Down
2 changes: 1 addition & 1 deletion ext/node/polyfills/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import {
} from "ext:deno_node/_next_tick.ts";
import { isWindows } from "ext:deno_node/_util/os.ts";
import * as io from "ext:deno_io/12_io.js";
import * as denoOs from "ext:runtime/30_os.js";
import * as denoOs from "ext:deno_os/30_os.js";

export let argv0 = "";

Expand Down
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions ext/os/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2018-2025 the Deno authors. MIT license.

[package]
name = "deno_os"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
readme = "README.md"
repository.workspace = true
description = "OS specific APIs for Deno"

[lib]
path = "lib.rs"

[dependencies]
deno_core.workspace = true
deno_error.workspace = true
deno_path_util.workspace = true
deno_permissions.workspace = true
deno_telemetry.workspace = true
libc.workspace = true
netif = "0.1.6"
once_cell.workspace = true
serde.workspace = true
signal-hook = "0.3.17"
signal-hook-registry = "1.4.0"
thiserror.workspace = true
tokio.workspace = true

[target.'cfg(windows)'.dependencies]
winapi = { workspace = true, features = ["commapi", "knownfolders", "mswsock", "objbase", "psapi", "shlobj", "tlhelp32", "winbase", "winerror", "winuser", "winsock2"] }
ntapi = "0.4.0"
4 changes: 3 additions & 1 deletion runtime/ops/os/README.md → ext/os/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## `os` ops
# deno_os

This crate implements OS specific APIs for Deno

`loadavg`

Expand Down
61 changes: 54 additions & 7 deletions runtime/ops/os/mod.rs → ext/os/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,54 @@
// Copyright 2018-2025 the Deno authors. MIT license.

use std::collections::HashMap;
use std::collections::HashSet;
use std::env;
use std::sync::atomic::AtomicI32;
use std::sync::atomic::Ordering;
use std::sync::Arc;

use deno_core::op2;
use deno_core::v8;
use deno_core::OpState;
use deno_node::NODE_ENV_VAR_ALLOWLIST;
use deno_path_util::normalize_path;
use deno_permissions::PermissionCheckError;
use deno_permissions::PermissionsContainer;
use once_cell::sync::Lazy;
use serde::Serialize;

use crate::sys_info;
use crate::worker::ExitCode;
mod ops;
pub mod signal;
pub mod sys_info;

pub use ops::signal::SignalError;

pub static NODE_ENV_VAR_ALLOWLIST: Lazy<HashSet<String>> = Lazy::new(|| {
// The full list of environment variables supported by Node.js is available
// at https://nodejs.org/api/cli.html#environment-variables
let mut set = HashSet::new();
set.insert("NODE_DEBUG".to_string());
set.insert("NODE_OPTIONS".to_string());
set
});

#[derive(Clone, Default)]
pub struct ExitCode(Arc<AtomicI32>);

impl ExitCode {
pub fn get(&self) -> i32 {
self.0.load(Ordering::Relaxed)
}

pub fn set(&mut self, code: i32) {
self.0.store(code, Ordering::Relaxed);
}
}

pub fn exit(code: i32) -> ! {
deno_telemetry::flush();
#[allow(clippy::disallowed_methods)]
std::process::exit(code);
}

deno_core::extension!(
deno_os,
Expand All @@ -35,13 +70,21 @@ deno_core::extension!(
op_system_memory_info,
op_uid,
op_runtime_memory_usage,
ops::signal::op_signal_bind,
ops::signal::op_signal_unbind,
ops::signal::op_signal_poll,
],
esm = ["30_os.js", "40_signals.js"],
options = {
exit_code: ExitCode,
},
state = |state, options| {
state.put::<ExitCode>(options.exit_code);
},
#[cfg(unix)]
{
state.put(ops::signal::SignalState::default());
}
}
);

deno_core::extension!(
Expand All @@ -64,12 +107,16 @@ deno_core::extension!(
op_system_memory_info,
op_uid,
op_runtime_memory_usage,
ops::signal::op_signal_bind,
ops::signal::op_signal_unbind,
ops::signal::op_signal_poll,
],
esm = ["30_os.js", "40_signals.js"],
middleware = |op| match op.name {
"op_exit" | "op_set_exit_code" | "op_get_exit_code" =>
op.with_implementation_from(&deno_core::op_void_sync()),
_ => op,
},
}
);

#[derive(Debug, thiserror::Error, deno_error::JsError)]
Expand Down Expand Up @@ -196,7 +243,7 @@ fn op_get_exit_code(state: &mut OpState) -> i32 {
#[op2(fast)]
fn op_exit(state: &mut OpState) {
let code = state.borrow::<ExitCode>().get();
crate::exit(code)
exit(code)
}

#[op2(stack_trace)]
Expand Down Expand Up @@ -239,7 +286,7 @@ fn op_network_interfaces(
Ok(netif::up()?.map(NetworkInterface::from).collect())
}

#[derive(serde::Serialize)]
#[derive(Serialize)]
struct NetworkInterface {
family: &'static str,
name: String,
Expand Down
3 changes: 3 additions & 0 deletions ext/os/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Copyright 2018-2025 the Deno authors. MIT license.

pub mod signal;
19 changes: 4 additions & 15 deletions runtime/ops/signal.rs → ext/os/ops/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ use tokio::signal::windows::CtrlBreak;
#[cfg(windows)]
use tokio::signal::windows::CtrlC;

deno_core::extension!(
deno_signal,
ops = [op_signal_bind, op_signal_unbind, op_signal_poll],
state = |state| {
#[cfg(unix)]
{
state.put(SignalState::default());
}
}
);

#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum SignalError {
#[class(type)]
Expand All @@ -62,7 +51,7 @@ pub enum SignalError {

#[cfg(unix)]
#[derive(Default)]
struct SignalState {
pub struct SignalState {
enable_default_handlers: BTreeMap<libc::c_int, Arc<AtomicBool>>,
}

Expand Down Expand Up @@ -164,7 +153,7 @@ impl Resource for SignalStreamResource {
#[cfg(unix)]
#[op2(fast)]
#[smi]
fn op_signal_bind(
pub fn op_signal_bind(
state: &mut OpState,
#[string] sig: &str,
) -> Result<ResourceId, SignalError> {
Expand Down Expand Up @@ -201,7 +190,7 @@ fn op_signal_bind(
#[cfg(windows)]
#[op2(fast)]
#[smi]
fn op_signal_bind(
pub fn op_signal_bind(
state: &mut OpState,
#[string] sig: &str,
) -> Result<ResourceId, SignalError> {
Expand All @@ -225,7 +214,7 @@ fn op_signal_bind(
}

#[op2(async)]
async fn op_signal_poll(
pub async fn op_signal_poll(
state: Rc<RefCell<OpState>>,
#[smi] rid: ResourceId,
) -> Result<bool, ResourceError> {
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 2 additions & 3 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ deno_core.workspace = true
deno_cron.workspace = true
deno_crypto.workspace = true
deno_fetch.workspace = true
deno_os.workspace = true
deno_ffi.workspace = true
deno_fs = { workspace = true, features = ["sync_fs"] }
deno_http.workspace = true
Expand Down Expand Up @@ -89,6 +90,7 @@ deno_kv.workspace = true
deno_napi.workspace = true
deno_net.workspace = true
deno_node.workspace = true
deno_os.workspace = true
deno_path_util.workspace = true
deno_permissions.workspace = true
deno_resolver.workspace = true
Expand All @@ -114,16 +116,13 @@ hyper-util.workspace = true
hyper_v014 = { workspace = true, features = ["server", "stream", "http1", "http2", "runtime"] }
libc.workspace = true
log.workspace = true
netif = "0.1.6"
notify.workspace = true
once_cell.workspace = true
percent-encoding.workspace = true
regex.workspace = true
rustyline = { workspace = true, features = ["custom-bindings"] }
same-file = "1.0.6"
serde.workspace = true
signal-hook = "0.3.17"
signal-hook-registry = "1.4.0"
sys_traits.workspace = true
tempfile.workspace = true
thiserror.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import * as version from "ext:runtime/01_version.ts";
import * as permissions from "ext:runtime/10_permissions.js";
import * as io from "ext:deno_io/12_io.js";
import * as fs from "ext:deno_fs/30_fs.js";
import * as os from "ext:runtime/30_os.js";
import * as os from "ext:deno_os/30_os.js";
import * as fsEvents from "ext:runtime/40_fs_events.js";
import * as process from "ext:runtime/40_process.js";
import * as signals from "ext:runtime/40_signals.js";
import * as signals from "ext:deno_os/40_signals.js";
import * as tty from "ext:runtime/40_tty.js";
import * as kv from "ext:deno_kv/01_db.ts";
import * as cron from "ext:deno_cron/01_cron.ts";
Expand Down
2 changes: 1 addition & 1 deletion runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { registerDeclarativeServer } from "ext:deno_http/00_serve.ts";
import * as event from "ext:deno_web/02_event.js";
import * as location from "ext:deno_web/12_location.js";
import * as version from "ext:runtime/01_version.ts";
import * as os from "ext:runtime/30_os.js";
import * as os from "ext:deno_os/30_os.js";
import * as timers from "ext:deno_web/02_timers.js";
import {
getDefaultInspectOptions,
Expand Down
Loading

0 comments on commit 974e2f4

Please sign in to comment.