Lesson 38-Deno Core Functionality Source Code Analysis

As a modern JavaScript/TypeScript runtime, Deno’s core functionality implementation showcases the design philosophy of deep integration between Rust and the V8 engine. This article provides an in-depth analysis of the source code mechanisms behind five key Deno functionalities.

Module System Source Code Analysis

ESM Module Loading Process

Key Source Code Paths:

  • cli/module_loader.ts – Main loading logic
  • cli/js/module_specifier.ts – URL parsing
  • cli/module_cache.ts – Cache management

Loading Process Source Code Implementation:

// cli/module_loader.rs
pub async fn load_module(
  specifier: &ModuleSpecifier,
  // ...
) -> Result<Module, AnyError> {
  // 1. URL parsing and normalization
  let resolved = resolve_specifier(specifier)?; 

  // 2. Cache check
  if let Some(cached) = module_cache.get(resolved.as_str()) {
    return Ok(cached);
  }

  // 3. Fetch source
  let source = fetch_source(resolved).await?;

  // 4. Compile module
  let compiled = compile_module(resolved, source).await?;

  // 5. Execute module
  let module = execute_module(compiled).await?;

  // 6. Cache result
  module_cache.set(resolved.as_str(), module.clone());

  Ok(module)
}

URL Parsing Key Code:

// cli/js/module_specifier.ts
export function resolveSpecifier(specifier: string): ModuleSpecifier {
  if (specifier.startsWith("file://")) {
    return resolveFileUrl(specifier);
  } else if (specifier.startsWith("http")) {
    return resolveHttpUrl(specifier);
  }
  // ...
}

Dynamic Import Implementation

Source Code Implementation:

// cli/ops/mod.rs
#[op]
fn op_dynamic_import(
  scope: &mut v8::HandleScope,
  specifier: v8::Local<v8::String>,
) -> Result<v8::Local<v8::Promise>, AnyError> {
  let specifier_str = specifier_to_string(scope, specifier)?;
  let promise = create_promise(scope)?;

  // Execute async loading in Tokio runtime
  tokio::spawn(async move {
    let result = module_loader.load_module(&specifier_str).await;
    // Resolve result to Promise
  });

  Ok(promise)
}

JavaScript Binding:

// cli/js/runtime.ts
Deno.core.opAsync("op_dynamic_import", (specifier: string) => {
  return import(specifier);
});

Module Caching Mechanism

Cache Data Structure:

// cli/module_cache.rs
struct ModuleCache {
  map: DashMap<String, ModuleEntry>, // Concurrent-safe hash map
  lru: LruCache<String, ()>,         // LRU cache strategy
}

struct ModuleEntry {
  module: Module,
  timestamp: Instant,
}

Cache Hit Check:

impl ModuleCache {
  pub fn get(&self, key: &str) -> Option<Module> {
    if let Some(entry) = self.map.get(key) {
      if self.lru.contains(key) {
        self.lru.get(key); // Update LRU position
        return Some(entry.module.clone());
      }
    }
    None
  }
}

Module Resolution Mechanism

Resolution Priority Strategy:

// cli/module_loader.rs
fn resolve_import(
  base: &ModuleSpecifier,
  specifier: &str,
) -> Result<ModuleSpecifier, AnyError> {
  // 1. Absolute URL returns directly
  if is_absolute_url(specifier) {
    return normalize_url(specifier);
  }

  // 2. Relative path resolution
  if specifier.startsWith("./") || specifier.startsWith("../") {
    return resolve_relative(base, specifier);
  }

  // 3. Node.js-style resolution (compatibility mode)
  if is_node_module(specifier) {
    return resolve_node_module(base, specifier);
  }

  // 4. Other cases throw error
  Err(generic_error("Invalid module specifier"))
}

Security Control Source Code

Permission Check Implementation:

// cli/module_loader.rs
fn check_import_permission(
  specifier: &ModuleSpecifier,
  permissions: &Permissions,
) -> Result<(), AnyError> {
  match specifier.scheme() {
    "file" => {
      let path = specifier.to_file_path().unwrap();
      permissions.read.check(&path)?;
    }
    "http" | "https" => {
      permissions.net.check_url(specifier)?;
    }
    _ => return Err(not_supported_error("Scheme not supported")),
  }
  Ok(())
}

Security Model Source Code Analysis

Permission System Implementation

Flag Parsing Source Code:

// cli/flags.rs
fn parse_allow_flags(flags: &[String]) -> PermissionsOptions {
  let mut options = PermissionsOptions::default();

  for flag in flags {
    match flag.as_str() {
      "--allow-read" => options.read = true,
      "--allow-write" => options.write = true,
      "--allow-net" => options.net = true,
      // ...other flag handling
    }
  }
  options
}

Permission Storage Structure:

// cli/permissions.rs
struct Permissions {
  read: PermissionState,
  write: PermissionState,
  net: PermissionState,
  // ...
}

enum PermissionState {
  Granted,
  Prompt,
  Denied,
}

Sandbox Mechanism Implementation

Resource Access Control:

// cli/sandbox.rs
impl Sandbox {
  pub fn check_file_access(&self, path: &Path) -> Result<(), Error> {
    if !self.allow_read.paths.iter().any(|p| path.starts_with(p)) {
      return Err(permission_denied("read"));
    }
    Ok(())
  }

  pub fn check_network_access(&self, host: &str) -> Result<(), Error> {
    if !self.allow_net.hosts.iter().any(|h| host.ends_with(h)) {
      return Err(permission_denied("net"));
    }
    Ok(())
  }
}

Dynamic Permission Requests

Permission API Implementation:

// cli/permissions.rs
#[op]
fn op_permissions_request(
  scope: &mut v8::HandleScope,
  kind: v8::Local<v8::String>,
) -> Result<v8::Local<v8::Promise>, AnyError> {
  let kind_str = v8_string_to_rust(scope, kind)?;
  let promise = create_promise(scope)?;

  tokio::spawn(async move {
    let result = permissions.request(kind_str).await;
    // Resolve result to Promise
  });

  Ok(promise)
}

JavaScript Binding:

// cli/js/permissions.ts
Deno.core.opAsync("op_permissions_request", async (kind: string) => {
  return await Deno.permissions.request({ name: kind });
});

Security Protection Source Code

Vulnerability Protection Measures:

// cli/js_errors.rs
fn sanitize_error_message(msg: &str) -> String {
  // Remove stack traces that may contain sensitive information
  msg.replace("/private/var/", "/sanitized/path/")
}

// CLI argument injection protection
fn validate_cmd_args(args: &[String]) -> Result<(), Error> {
  if args.iter().any(|a| a.contains(";")) {
    return Err(illegal_argument("Potential command injection"));
  }
  Ok(())
}

Performance Optimization

Permission Cache Implementation:

// cli/permissions.rs
struct PermissionCache {
  read: LruCache<PathBuf, bool>,
  net: LruCache<String, bool>,
}

impl PermissionCache {
  pub fn check_read(&mut self, path: &Path) -> bool {
    if let Some(cached) = self.read.get(path) {
      return *cached;
    }
    // Actual check logic...
  }
}

Asynchronous I/O Source Code Analysis

Tokio Integration Mechanism

Runtime Initialization:

// cli/runtime.rs
fn create_runtime() -> tokio::runtime::Runtime {
  tokio::runtime::Builder::new_multi_thread()
    .worker_threads(num_cpus::get())
    .enable_io()
    .enable_time()
    .build()
    .unwrap()
}

Task Scheduling Source Code:

// cli/ops/mod.rs
#[op]
fn op_spawn_task(
  scope: &mut v8::HandleScope,
  callback: v8::Local<v8::Function>,
) -> Result<v8::Local<v8::Promise>, AnyError> {
  let future = async move {
    let result = callback.call(scope, ...).await;
    // Result handling...
  };

  tokio::spawn(future);
  // Return Promise...
}

File System Asynchronous Implementation

File Operation Source Code:

// cli/fs/read.rs
pub async fn read_file(path: &Path) -> Result<Vec<u8>, Error> {
  let file = tokio::fs::File::open(path).await?;
  let mut buf = Vec::new();
  tokio::io::AsyncReadExt::read_to_end(&mut file, &mut buf).await?;
  Ok(buf)
}

Buffer Strategy Optimization:

// cli/fs/buf_reader.rs
struct BufReader {
  inner: tokio::fs::File,
  buf: [u8; 8192], // 8KB buffer
  pos: usize,
  cap: usize,
}

Network Request Implementation

TCP Client Source Code:

// cli/net/tcp.rs
pub async fn connect_tcp(addr: SocketAddr) -> Result<TcpStream, Error> {
  let stream = tokio::net::TcpStream::connect(addr).await?;
  Ok(stream)
}

HTTP Client Implementation:

// cli/http/client.rs
pub async fn fetch(url: &str) -> Result<Response, Error> {
  let req = Request::builder()
    .uri(url)
    .body(Body::empty())?;

  let client = hyper::Client::new();
  let res = client.request(req).await?;
  Ok(res)
}

Timer Implementation

Timer Source Code:

// cli/async_ops.rs
#[op]
fn op_set_timeout(
  scope: &mut v8::HandleScope,
  delay: u32,
) -> Result<v8::Local<v8::Promise>, AnyError> {
  let promise = create_promise(scope)?;

  tokio::spawn(async move {
    tokio::time::sleep(Duration::from_millis(delay as u64)).await;
    // Resolve Promise...
  });

  Ok(promise)
}

High-Precision Timer Optimization:

// cli/runtime.rs
fn create_high_res_timer() -> tokio::time::Interval {
  tokio::time::interval_at(
    tokio::time::Instant::now() + Duration::from_millis(1),
    Duration::from_millis(1)
  )
}

Exception Handling Source Code

Exception Capture Mechanism:

// cli/ops/mod.rs
#[op]
fn op_try_catch(
  scope: &mut v8::HandleScope,
  callback: v8::Local<v8::Function>,
) -> Result<v8::Local<v8::Value>, AnyError> {
  let try_catch = v8::TryCatch::new(scope);

  callback.call(scope, ...);

  if try_catch.has_caught() {
    let exception = try_catch.exception();
    // Convert exception to Rust error...
  }
  // ...
}

Promise Error Handling:

// cli/js/promise.ts
Deno.core.opAsync("op_promise_then", async (promiseId: number) => {
  try {
    const result = await getPromise(promiseId);
    return { status: "fulfilled", value: result };
  } catch (err) {
    return { status: "rejected", reason: err.message };
  }
});
Share your love