deno_node 0.182.0

Node compatibility for Deno
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Copyright 2018-2026 the Deno authors. MIT license.

use std::cell::Cell;
use std::cell::RefCell;
use std::rc::Rc;

use deno_core::CppgcBase;
use deno_core::CppgcInherits;
use deno_core::GarbageCollected;
use deno_core::OpState;
use deno_core::ResourceId;
use deno_core::error::ResourceError;
use deno_core::op2;
use deno_core::uv_compat;
use deno_core::uv_compat::uv_handle_t;
use deno_core::v8;

// ---------------------------------------------------------------------------
// GlobalHandle — mirrors Node's BaseObject::persistent_handle_ weak/strong
// switching.
//
// In Node, BaseObject holds a v8::Global that can be switched between strong
// (prevents GC of the JS object) and weak (allows GC, triggering cleanup).
// With cppgc the C++ object's lifetime is managed by the GC, but we still
// need to hold a reference back to the JS wrapper object for use in
// callbacks. A strong Global would create a reference cycle (JS -> cppgc
// -> Global -> JS) and leak. A Weak reference allows the GC to collect
// the pair when nothing else references them.
//
// The handle starts Strong after construction (the active libuv handle
// should keep the object alive). It should be made Weak when the handle
// is closed or no longer actively referenced.
// ---------------------------------------------------------------------------

/// A reference to a JS object that can switch between strong (GC root),
/// weak (allows collection), or empty. This mirrors Node's pattern of
/// calling `MakeWeak()` / `ClearWeak()` on `BaseObject::persistent_handle_`.
#[derive(Default)]
pub enum GlobalHandle<T> {
  Strong(v8::Global<T>),
  Weak(v8::Weak<T>),
  #[default]
  None,
}

impl<T> GlobalHandle<T>
where
  v8::Global<T>: v8::Handle<Data = T>,
{
  /// Create a new strong handle.
  pub fn new_strong(scope: &mut v8::PinScope, value: v8::Local<T>) -> Self {
    GlobalHandle::Strong(v8::Global::new(scope, value))
  }

  /// Create a new weak handle.
  pub fn new_weak(scope: &mut v8::PinScope, value: v8::Local<T>) -> Self {
    GlobalHandle::Weak(v8::Weak::new(scope, value))
  }

  /// Make the handle weak, allowing the GC to collect the JS object.
  /// Mirrors Node's `BaseObject::MakeWeak()`.
  pub fn make_weak(&mut self, scope: &mut v8::PinScope) {
    match std::mem::take(self) {
      GlobalHandle::Strong(global) => {
        let local = v8::Local::new(scope, &global);
        *self = GlobalHandle::Weak(v8::Weak::new(scope, local));
      }
      other => *self = other,
    }
  }

  /// Make the handle strong, preventing the GC from collecting the JS object.
  /// Mirrors Node's `BaseObject::ClearWeak()`.
  pub fn make_strong(&mut self, scope: &mut v8::PinScope) {
    match std::mem::take(self) {
      GlobalHandle::Weak(weak) => {
        if let Some(local) = weak.to_local(scope) {
          *self = GlobalHandle::Strong(v8::Global::new(scope, local));
        }
        // If already collected, stays None
      }
      other => *self = other,
    }
  }

  /// Get a Global clone if the reference is still alive.
  /// Returns None if empty or if the weak reference has been collected.
  pub fn to_global(&self, scope: &mut v8::PinScope) -> Option<v8::Global<T>> {
    match self {
      GlobalHandle::Strong(global) => Some(global.clone()),
      GlobalHandle::Weak(weak) => weak
        .to_local(scope)
        .map(|local| v8::Global::new(scope, local)),
      GlobalHandle::None => None,
    }
  }

  /// Returns true if this is a weak reference.
  pub fn is_weak(&self) -> bool {
    matches!(self, GlobalHandle::Weak(_))
  }

  /// Returns true if this handle is empty.
  pub fn is_none(&self) -> bool {
    matches!(self, GlobalHandle::None)
  }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum ProviderType {
  None = 0,
  DirHandle,
  DnsChannel,
  EldHistogram,
  FileHandle,
  FileHandleCloseReq,
  FixedSizeBlobCopy,
  FsEventWrap,
  FsReqCallback,
  FsReqPromise,
  GetAddrInfoReqWrap,
  GetNameInfoReqWrap,
  HeapSnapshot,
  Http2Session,
  Http2Stream,
  Http2Ping,
  Http2Settings,
  HttpIncomingMessage,
  HttpClientRequest,
  JsStream,
  JsUdpWrap,
  MessagePort,
  PipeConnectWrap,
  PipeServerWrap,
  PipeWrap,
  ProcessWrap,
  Promise,
  QueryWrap,
  ShutdownWrap,
  SignalWrap,
  StatWatcher,
  StreamPipe,
  TcpConnectWrap,
  TcpServerWrap,
  TcpWrap,
  TtyWrap,
  UdpSendWrap,
  UdpWrap,
  SigIntWatchdog,
  Worker,
  WorkerHeapSnapshot,
  WriteWrap,
  Zlib,
}

impl From<ProviderType> for i32 {
  fn from(provider: ProviderType) -> Self {
    provider as i32
  }
}

pub use deno_core::uv_compat::AsyncId;

fn next_async_id(state: &mut OpState) -> i64 {
  state.borrow_mut::<AsyncId>().next()
}

#[op2(fast)]
pub fn op_node_new_async_id(state: &mut OpState) -> f64 {
  next_async_id(state) as f64
}

#[derive(CppgcBase)]
#[repr(C)]
pub struct AsyncWrap {
  provider: i32,
  async_id: i64,
}

// SAFETY: we're sure this can be GCed
unsafe impl GarbageCollected for AsyncWrap {
  fn trace(&self, _visitor: &mut deno_core::v8::cppgc::Visitor) {}

  fn get_name(&self) -> &'static std::ffi::CStr {
    c"AsyncWrap"
  }
}

impl AsyncWrap {
  pub(crate) fn create(state: &mut OpState, provider: i32) -> Self {
    let async_id = next_async_id(state);

    Self { provider, async_id }
  }
}

#[op2(base)]
impl AsyncWrap {
  #[getter]
  fn provider(&self) -> i32 {
    self.provider
  }

  #[fast]
  fn get_async_id(&self) -> f64 {
    self.async_id as f64
  }

  #[fast]
  fn get_provider_type(&self) -> i32 {
    self.provider
  }
}

#[derive(Copy, Clone, PartialEq, Eq, Default)]
enum State {
  #[default]
  Initialized,
  Closing,
  Closed,
}

/// A handle to a libuv resource. `New` stores a raw pointer to a `uv_handle_t`
/// whose lifetime is managed by the owning cppgc object (e.g. `TTY`). The
/// pointer is valid as long as the handle has not been closed via `uv_close`.
/// This mirrors Node's approach where `HandleWrap` stores a `uv_handle_t*`
/// that becomes null after close.
#[derive(PartialEq, Eq)]
pub enum Handle {
  Old(ResourceId),
  New(*const uv_handle_t),
}

#[derive(CppgcBase, CppgcInherits)]
#[cppgc_inherits_from(AsyncWrap)]
#[repr(C)]
pub struct HandleWrap {
  base: AsyncWrap,
  handle: Option<Handle>,
  state: Rc<Cell<State>>,
}

// SAFETY: we're sure this can be GCed
unsafe impl GarbageCollected for HandleWrap {
  fn trace(&self, _visitor: &mut deno_core::v8::cppgc::Visitor) {}

  fn get_name(&self) -> &'static std::ffi::CStr {
    c"HandleWrap"
  }
}

impl HandleWrap {
  pub(crate) fn create(base: AsyncWrap, handle: Option<Handle>) -> Self {
    Self {
      base,
      handle,
      state: Rc::new(Cell::new(State::Initialized)),
    }
  }

  fn is_alive(&self) -> bool {
    self.state.get() != State::Closed
  }
}

static ON_CLOSE_STR: deno_core::FastStaticString =
  deno_core::ascii_str!("_onClose");

#[op2(inherit = AsyncWrap, base)]
impl HandleWrap {
  #[constructor]
  #[cppgc]
  fn new(
    state: &mut OpState,
    #[smi] provider: i32,
    #[smi] handle: Option<ResourceId>,
  ) -> HandleWrap {
    HandleWrap::create(
      AsyncWrap::create(state, provider),
      handle.map(Handle::Old),
    )
  }

  // Ported from Node.js
  //
  // https://github.com/nodejs/node/blob/038d82980ab26cd79abe4409adc2fecad94d7c93/src/handle_wrap.cc#L65-L85
  #[reentrant]
  fn close(
    &self,
    op_state: Rc<RefCell<OpState>>,
    #[this] this: v8::Global<v8::Object>,
    scope: &mut v8::PinScope<'_, '_>,
    #[scoped] cb: Option<v8::Global<v8::Function>>,
  ) -> Result<(), ResourceError> {
    if self.state.get() != State::Initialized {
      return Ok(());
    }

    let state = self.state.clone();
    // This effectively mimicks Node's OnClose callback.
    //
    // https://github.com/nodejs/node/blob/038d82980ab26cd79abe4409adc2fecad94d7c93/src/handle_wrap.cc#L135-L157
    let on_close = move |scope: &mut v8::PinScope<'_, '_>| {
      assert!(state.get() == State::Closing);
      state.set(State::Closed);

      // Workaround for https://github.com/denoland/deno/pull/24656
      //
      // We need to delay 'cb' at least 2 ticks to avoid "close" event happening before "error"
      // event in net.Socket.
      //
      // This is a temporary solution. We should support async close like `uv_close`.
      if let Some(cb) = cb {
        let recv = v8::undefined(scope);
        cb.open(scope).call(scope, recv.into(), &[]);
      }
    };

    // For new-style handles (uv_compat), call uv_compat::uv_close to
    // properly shut down the libuv handle (e.g. close FDs for TTY).
    // Without this, the libuv handle cleanup never runs and resources
    // like PTY master file descriptors are leaked.
    if let Some(Handle::New(handle)) = &self.handle {
      // SAFETY: handle is a valid uv_handle_t pointer set during
      // construction and remains live while HandleWrap is alive.
      unsafe {
        uv_compat::uv_close(handle.cast_mut(), None);
      }
    }

    uv_close(scope, op_state, this, on_close);
    self.state.set(State::Closing);

    Ok(())
  }

  // Ported from Node.js
  //
  // https://github.com/nodejs/node/blob/038d82980ab26cd79abe4409adc2fecad94d7c93/src/handle_wrap.cc#L58-L62
  #[fast]
  fn has_ref(&self, state: &mut OpState) -> bool {
    if let Some(handle) = &self.handle {
      return match handle {
        Handle::Old(resource_id) => state.has_ref(*resource_id),
        // SAFETY: handle is a valid uv_handle_t pointer set during construction and remains live while HandleWrap is alive.
        Handle::New(handle) => unsafe { uv_compat::uv_has_ref(*handle) != 0 },
      };
    }

    true
  }

  // Ported from Node.js
  //
  // https://github.com/nodejs/node/blob/038d82980ab26cd79abe4409adc2fecad94d7c93/src/handle_wrap.cc#L40-L46
  #[fast]
  #[rename("ref")]
  fn ref_method(&self, state: &mut OpState) {
    if self.is_alive()
      && let Some(handle) = &self.handle
    {
      match handle {
        Handle::Old(resource_id) => state.uv_ref(*resource_id),
        // SAFETY: handle is a valid uv_handle_t pointer set during construction and remains live while HandleWrap is alive.
        Handle::New(handle) => unsafe { uv_compat::uv_ref(handle.cast_mut()) },
      }
    }
  }

  // Ported from Node.js
  //
  // https://github.com/nodejs/node/blob/038d82980ab26cd79abe4409adc2fecad94d7c93/src/handle_wrap.cc#L49-L55
  #[fast]
  fn unref(&self, state: &mut OpState) {
    if self.is_alive()
      && let Some(handle) = &self.handle
    {
      match handle {
        Handle::Old(resource_id) => state.uv_unref(*resource_id),
        // SAFETY: handle is a valid uv_handle_t pointer set during construction and remains live while HandleWrap is alive.
        Handle::New(handle) => unsafe {
          uv_compat::uv_unref(handle.cast_mut())
        },
      }
    }
  }
}

fn uv_close<F>(
  scope: &mut v8::PinScope<'_, '_>,
  op_state: Rc<RefCell<OpState>>,
  this: v8::Global<v8::Object>,
  on_close: F,
) where
  F: FnOnce(&mut v8::PinScope<'_, '_>) + 'static,
{
  // Call _onClose() on the JS handles. Not needed for Rust handles.
  let this = v8::Local::new(scope, this);
  let on_close_str = ON_CLOSE_STR.v8_string(scope).unwrap();
  let onclose = this.get(scope, on_close_str.into());

  if let Some(onclose) = onclose
    && let Ok(fn_) = v8::Local::<v8::Function>::try_from(onclose)
  {
    fn_.call(scope, this.into(), &[]);
  }

  op_state
    .borrow()
    .borrow::<deno_core::V8TaskSpawner>()
    .spawn(on_close);
}

#[cfg(test)]
mod tests {
  use std::future::poll_fn;
  use std::task::Poll;

  use deno_core::JsRuntime;
  use deno_core::RuntimeOptions;

  async fn js_test(source_code: &'static str) {
    deno_core::extension!(
      test_ext,
      objects = [super::AsyncWrap, super::HandleWrap,],
      state = |state| {
        state.put::<super::AsyncId>(super::AsyncId::default());
      }
    );

    let mut runtime = JsRuntime::new(RuntimeOptions {
      extensions: vec![test_ext::init()],
      ..Default::default()
    });

    poll_fn(move |cx| {
      runtime
        .execute_script("file://handle_wrap_test.js", source_code)
        .unwrap();

      let result = runtime.poll_event_loop(cx, Default::default());
      assert!(matches!(result, Poll::Ready(Ok(()))));
      Poll::Ready(())
    })
    .await;
  }

  #[tokio::test]
  async fn test_handle_wrap() {
    js_test(
      r#"
        const { HandleWrap } = Deno.core.ops;

        let called = false;
        class MyHandleWrap extends HandleWrap {
          constructor() {
            super(0, null);
          }

          _onClose() {
            called = true;
          }
        }

        const handleWrap = new MyHandleWrap();
        handleWrap.close();

        if (!called) {
          throw new Error("HandleWrap._onClose was not called");
        }
      "#,
    )
    .await;
  }
}