deno_core/io/resource.rs
1// Copyright 2018-2025 the Deno authors. MIT license.
2
3// Think of Resources as File Descriptors. They are integers that are allocated
4// by the privileged side of Deno which refer to various rust objects that need
5// to be persisted between various ops. For example, network sockets are
6// resources. Resources may or may not correspond to a real operating system
7// file descriptor (hence the different name).
8
9use crate::ResourceHandle;
10use crate::ResourceHandleFd;
11use crate::io::AsyncResult;
12use crate::io::BufMutView;
13use crate::io::BufView;
14use crate::io::WriteOutcome;
15use deno_error::JsErrorBox;
16use deno_error::JsErrorClass;
17use std::any::Any;
18use std::any::TypeId;
19use std::any::type_name;
20use std::borrow::Cow;
21use std::rc::Rc;
22
23/// Resources are Rust objects that are attached to a [deno_core::JsRuntime].
24/// They are identified in JS by a numeric ID (the resource ID, or rid).
25/// Resources can be created in ops. Resources can also be retrieved in ops by
26/// their rid. Resources are not thread-safe - they can only be accessed from
27/// the thread that the JsRuntime lives on.
28///
29/// Resources are reference counted in Rust. This means that they can be
30/// cloned and passed around. When the last reference is dropped, the resource
31/// is automatically closed. As long as the resource exists in the resource
32/// table, the reference count is at least 1.
33///
34/// ### Readable
35///
36/// Readable resources are resources that can have data read from. Examples of
37/// this are files, sockets, or HTTP streams.
38///
39/// Readables can be read from from either JS or Rust. In JS one can use
40/// `Deno.core.read()` to read from a single chunk of data from a readable. In
41/// Rust one can directly call `read()` or `read_byob()`. The Rust side code is
42/// used to implement ops like `op_slice`.
43///
44/// A distinction can be made between readables that produce chunks of data
45/// themselves (they allocate the chunks), and readables that fill up
46/// bring-your-own-buffers (BYOBs). The former is often the case for framed
47/// protocols like HTTP, while the latter is often the case for kernel backed
48/// resources like files and sockets.
49///
50/// All readables must implement `read()`. If resources can support an optimized
51/// path for BYOBs, they should also implement `read_byob()`. For kernel backed
52/// resources it often makes sense to implement `read_byob()` first, and then
53/// implement `read()` as an operation that allocates a new chunk with
54/// `len == limit`, then calls `read_byob()`, and then returns a chunk sliced to
55/// the number of bytes read. Kernel backed resources can use the
56/// [deno_core::impl_readable_byob] macro to implement optimized `read_byob()`
57/// and `read()` implementations from a single `Self::read()` method.
58///
59/// ### Writable
60///
61/// Writable resources are resources that can have data written to. Examples of
62/// this are files, sockets, or HTTP streams.
63///
64/// Writables can be written to from either JS or Rust. In JS one can use
65/// `Deno.core.write()` to write to a single chunk of data to a writable. In
66/// Rust one can directly call `write()`. The latter is used to implement ops
67/// like `op_slice`.
68pub trait Resource: Any + 'static {
69 /// Returns a string representation of the resource which is made available
70 /// to JavaScript code through `op_resources`. The default implementation
71 /// returns the Rust type name, but specific resource types may override this
72 /// trait method.
73 fn name(&self) -> Cow<str> {
74 type_name::<Self>().into()
75 }
76
77 /// Read a single chunk of data from the resource. This operation returns a
78 /// `BufView` that represents the data that was read. If a zero length buffer
79 /// is returned, it indicates that the resource has reached EOF.
80 ///
81 /// If this method is not implemented, the default implementation will error
82 /// with a "not supported" error.
83 ///
84 /// If a readable can provide an optimized path for BYOBs, it should also
85 /// implement `read_byob()`.
86 fn read(self: Rc<Self>, limit: usize) -> AsyncResult<BufView> {
87 _ = limit;
88 Box::pin(std::future::ready(Err(JsErrorBox::not_supported())))
89 }
90
91 /// Read a single chunk of data from the resource into the provided `BufMutView`.
92 ///
93 /// This operation returns the number of bytes read. If zero bytes are read,
94 /// it indicates that the resource has reached EOF.
95 ///
96 /// If this method is not implemented explicitly, the default implementation
97 /// will call `read()` and then copy the data into the provided buffer. For
98 /// readable resources that can provide an optimized path for BYOBs, it is
99 /// strongly recommended to override this method.
100 fn read_byob(
101 self: Rc<Self>,
102 mut buf: BufMutView,
103 ) -> AsyncResult<(usize, BufMutView)> {
104 Box::pin(async move {
105 let read = self.read(buf.len()).await?;
106 let nread = read.len();
107 buf[..nread].copy_from_slice(&read);
108 Ok((nread, buf))
109 })
110 }
111
112 /// Write an error state to this resource, if the resource supports it.
113 fn write_error(self: Rc<Self>, _error: &dyn JsErrorClass) -> AsyncResult<()> {
114 Box::pin(std::future::ready(Err(JsErrorBox::not_supported())))
115 }
116
117 /// Write a single chunk of data to the resource. The operation may not be
118 /// able to write the entire chunk, in which case it should return the number
119 /// of bytes written. Additionally it should return the `BufView` that was
120 /// passed in.
121 ///
122 /// If this method is not implemented, the default implementation will error
123 /// with a "not supported" error.
124 fn write(self: Rc<Self>, buf: BufView) -> AsyncResult<WriteOutcome> {
125 _ = buf;
126 Box::pin(std::future::ready(Err(JsErrorBox::not_supported())))
127 }
128
129 /// Write an entire chunk of data to the resource. Unlike `write()`, this will
130 /// ensure the entire chunk is written. If the operation is not able to write
131 /// the entire chunk, an error is to be returned.
132 ///
133 /// By default this method will call `write()` repeatedly until the entire
134 /// chunk is written. Resources that can write the entire chunk in a single
135 /// operation using an optimized path should override this method.
136 fn write_all(self: Rc<Self>, view: BufView) -> AsyncResult<()> {
137 Box::pin(async move {
138 let mut view = view;
139 let this = self;
140 while !view.is_empty() {
141 let resp = this.clone().write(view).await?;
142 match resp {
143 WriteOutcome::Partial {
144 nwritten,
145 view: new_view,
146 } => {
147 view = new_view;
148 view.advance_cursor(nwritten);
149 }
150 WriteOutcome::Full { .. } => break,
151 }
152 }
153 Ok(())
154 })
155 }
156
157 /// The same as [`read_byob()`][Resource::read_byob], but synchronous.
158 fn read_byob_sync(
159 self: Rc<Self>,
160 data: &mut [u8],
161 ) -> Result<usize, JsErrorBox> {
162 _ = data;
163 Err(JsErrorBox::not_supported())
164 }
165
166 /// The same as [`write()`][Resource::write], but synchronous.
167 fn write_sync(self: Rc<Self>, data: &[u8]) -> Result<usize, JsErrorBox> {
168 _ = data;
169 Err(JsErrorBox::not_supported())
170 }
171
172 /// The shutdown method can be used to asynchronously close the resource. It
173 /// is not automatically called when the resource is dropped or closed.
174 ///
175 /// If this method is not implemented, the default implementation will error
176 /// with a "not supported" error.
177 fn shutdown(self: Rc<Self>) -> AsyncResult<()> {
178 Box::pin(std::future::ready(Err(JsErrorBox::not_supported())))
179 }
180
181 /// Resources may implement the `close()` trait method if they need to do
182 /// resource specific clean-ups, such as cancelling pending futures, after a
183 /// resource has been removed from the resource table.
184 fn close(self: Rc<Self>) {}
185
186 /// Resources backed by a file descriptor or socket handle can let ops know
187 /// to allow for low-level optimizations.
188 fn backing_handle(self: Rc<Self>) -> Option<ResourceHandle> {
189 #[allow(deprecated)]
190 self.backing_fd().map(ResourceHandle::Fd)
191 }
192
193 /// Resources backed by a file descriptor can let ops know to allow for
194 /// low-level optimizations.
195 #[deprecated = "Use backing_handle"]
196 fn backing_fd(self: Rc<Self>) -> Option<ResourceHandleFd> {
197 None
198 }
199
200 fn size_hint(&self) -> (u64, Option<u64>) {
201 (0, None)
202 }
203}
204
205impl dyn Resource {
206 #[inline(always)]
207 fn is<T: Resource>(&self) -> bool {
208 self.type_id() == TypeId::of::<T>()
209 }
210
211 #[inline(always)]
212 #[allow(clippy::needless_lifetimes)]
213 pub fn downcast_rc<'a, T: Resource>(self: &'a Rc<Self>) -> Option<&'a Rc<T>> {
214 if self.is::<T>() {
215 let ptr = self as *const Rc<_> as *const Rc<T>;
216 // TODO(piscisaureus): safety comment
217 #[allow(clippy::undocumented_unsafe_blocks)]
218 Some(unsafe { &*ptr })
219 } else {
220 None
221 }
222 }
223}
224
225#[macro_export]
226macro_rules! impl_readable_byob {
227 () => {
228 fn read(
229 self: ::std::rc::Rc<Self>,
230 limit: ::core::primitive::usize,
231 ) -> AsyncResult<$crate::BufView> {
232 ::std::boxed::Box::pin(async move {
233 let mut vec = ::std::vec![0; limit];
234 let nread = self.read(&mut vec).await.map_err(::deno_error::JsErrorBox::from_err)?;
235 if nread != vec.len() {
236 vec.truncate(nread);
237 }
238 let view = $crate::BufView::from(vec);
239 ::std::result::Result::Ok(view)
240 })
241 }
242
243 fn read_byob(
244 self: ::std::rc::Rc<Self>,
245 mut buf: $crate::BufMutView,
246 ) -> AsyncResult<(::core::primitive::usize, $crate::BufMutView)> {
247 ::std::boxed::Box::pin(async move {
248 let nread = self.read(buf.as_mut()).await.map_err(::deno_error::JsErrorBox::from_err)?;
249 ::std::result::Result::Ok((nread, buf))
250 })
251 }
252 };
253}
254
255#[macro_export]
256macro_rules! impl_writable {
257 (__write) => {
258 fn write(
259 self: ::std::rc::Rc<Self>,
260 view: $crate::BufView,
261 ) -> $crate::AsyncResult<$crate::WriteOutcome> {
262 ::std::boxed::Box::pin(async move {
263 let nwritten = self
264 .write(&view)
265 .await
266 .map_err(::deno_error::JsErrorBox::from_err)?;
267 ::std::result::Result::Ok($crate::WriteOutcome::Partial {
268 nwritten,
269 view,
270 })
271 })
272 }
273 };
274 (__write_all) => {
275 fn write_all(
276 self: ::std::rc::Rc<Self>,
277 view: $crate::BufView,
278 ) -> $crate::AsyncResult<()> {
279 ::std::boxed::Box::pin(async move {
280 self
281 .write_all(&view)
282 .await
283 .map_err(::deno_error::JsErrorBox::from_err)?;
284 ::std::result::Result::Ok(())
285 })
286 }
287 };
288 () => {
289 $crate::impl_writable!(__write);
290 };
291 (with_all) => {
292 $crate::impl_writable!(__write);
293 $crate::impl_writable!(__write_all);
294 };
295}