2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2021-07-06 23:48:01 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
|
|
|
use deno_core::CancelFuture;
|
|
|
|
use deno_core::CancelHandle;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::RcRef;
|
|
|
|
use deno_core::Resource;
|
2021-03-19 13:25:37 -04:00
|
|
|
use deno_core::ResourceId;
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::Extension;
|
2020-02-21 13:21:51 -05:00
|
|
|
use notify::event::Event as NotifyEvent;
|
|
|
|
use notify::Error as NotifyError;
|
|
|
|
use notify::EventKind;
|
|
|
|
use notify::RecommendedWatcher;
|
|
|
|
use notify::RecursiveMode;
|
|
|
|
use notify::Watcher;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-02-21 13:21:51 -05:00
|
|
|
use serde::Serialize;
|
2020-12-16 11:14:12 -05:00
|
|
|
use std::borrow::Cow;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2020-02-21 13:21:51 -05:00
|
|
|
use std::convert::From;
|
|
|
|
use std::path::PathBuf;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2020-02-21 13:21:51 -05:00
|
|
|
use tokio::sync::mpsc;
|
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
pub fn init() -> Extension {
|
|
|
|
Extension::builder()
|
2022-03-14 13:44:15 -04:00
|
|
|
.ops(vec![op_fs_events_open::decl(), op_fs_events_poll::decl()])
|
2021-05-02 19:22:57 -04:00
|
|
|
.build()
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct FsEventsResource {
|
|
|
|
#[allow(unused)]
|
|
|
|
watcher: RecommendedWatcher,
|
2020-12-16 11:14:12 -05:00
|
|
|
receiver: AsyncRefCell<mpsc::Receiver<Result<FsEvent, AnyError>>>,
|
|
|
|
cancel: CancelHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for FsEventsResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"fsEvents".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel();
|
|
|
|
}
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a file system event.
|
|
|
|
///
|
|
|
|
/// We do not use the event directly from the notify crate. We flatten
|
|
|
|
/// the structure into this simpler structure. We want to only make it more
|
|
|
|
/// complex as needed.
|
|
|
|
///
|
|
|
|
/// Feel free to expand this struct as long as you can add tests to demonstrate
|
|
|
|
/// the complexity.
|
|
|
|
#[derive(Serialize, Debug)]
|
|
|
|
struct FsEvent {
|
2021-11-23 05:30:24 -05:00
|
|
|
kind: &'static str,
|
2020-02-21 13:21:51 -05:00
|
|
|
paths: Vec<PathBuf>,
|
2021-11-23 05:30:24 -05:00
|
|
|
flag: Option<&'static str>,
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<NotifyEvent> for FsEvent {
|
|
|
|
fn from(e: NotifyEvent) -> Self {
|
|
|
|
let kind = match e.kind {
|
|
|
|
EventKind::Any => "any",
|
|
|
|
EventKind::Access(_) => "access",
|
|
|
|
EventKind::Create(_) => "create",
|
|
|
|
EventKind::Modify(_) => "modify",
|
|
|
|
EventKind::Remove(_) => "remove",
|
2021-11-23 05:30:24 -05:00
|
|
|
EventKind::Other => "other",
|
|
|
|
};
|
|
|
|
let flag = e.flag().map(|f| match f {
|
|
|
|
notify::event::Flag::Rescan => "rescan",
|
|
|
|
});
|
2020-02-21 13:21:51 -05:00
|
|
|
FsEvent {
|
|
|
|
kind,
|
|
|
|
paths: e.paths,
|
2021-11-23 05:30:24 -05:00
|
|
|
flag,
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 14:42:01 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct OpenArgs {
|
|
|
|
recursive: bool,
|
|
|
|
paths: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_fs_events_open(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: OpenArgs,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<ResourceId, AnyError> {
|
2020-09-14 12:48:57 -04:00
|
|
|
let (sender, receiver) = mpsc::channel::<Result<FsEvent, AnyError>>(16);
|
2021-07-06 23:48:01 -04:00
|
|
|
let sender = Mutex::new(sender);
|
2020-02-21 13:21:51 -05:00
|
|
|
let mut watcher: RecommendedWatcher =
|
2021-09-02 17:38:44 -04:00
|
|
|
Watcher::new(move |res: Result<NotifyEvent, NotifyError>| {
|
2020-09-14 12:48:57 -04:00
|
|
|
let res2 = res.map(FsEvent::from).map_err(AnyError::from);
|
2021-07-06 23:48:01 -04:00
|
|
|
let sender = sender.lock();
|
2020-03-05 20:01:08 -05:00
|
|
|
// Ignore result, if send failed it means that watcher was already closed,
|
|
|
|
// but not all messages have been flushed.
|
2020-04-15 23:14:28 -04:00
|
|
|
let _ = sender.try_send(res2);
|
2020-08-25 18:22:15 -04:00
|
|
|
})?;
|
2020-02-21 13:21:51 -05:00
|
|
|
let recursive_mode = if args.recursive {
|
|
|
|
RecursiveMode::Recursive
|
|
|
|
} else {
|
|
|
|
RecursiveMode::NonRecursive
|
|
|
|
};
|
|
|
|
for path in &args.paths {
|
2021-09-02 17:38:44 -04:00
|
|
|
let path = PathBuf::from(path);
|
|
|
|
state.borrow_mut::<Permissions>().read.check(&path)?;
|
|
|
|
watcher.watch(&path, recursive_mode)?;
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = FsEventsResource {
|
|
|
|
watcher,
|
|
|
|
receiver: AsyncRefCell::new(receiver),
|
|
|
|
cancel: Default::default(),
|
|
|
|
};
|
|
|
|
let rid = state.resource_table.add(resource);
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(rid)
|
2021-03-18 14:42:01 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_fs_events_poll(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<Option<FsEvent>, AnyError> {
|
2021-08-15 07:29:19 -04:00
|
|
|
let resource = state.borrow().resource_table.get::<FsEventsResource>(rid)?;
|
2020-12-16 11:14:12 -05:00
|
|
|
let mut receiver = RcRef::map(&resource, |r| &r.receiver).borrow_mut().await;
|
|
|
|
let cancel = RcRef::map(resource, |r| &r.cancel);
|
|
|
|
let maybe_result = receiver.recv().or_cancel(cancel).await?;
|
|
|
|
match maybe_result {
|
2021-04-05 12:40:24 -04:00
|
|
|
Some(Ok(value)) => Ok(Some(value)),
|
2020-12-16 11:14:12 -05:00
|
|
|
Some(Err(err)) => Err(err),
|
2021-04-05 12:40:24 -04:00
|
|
|
None => Ok(None),
|
2020-12-16 11:14:12 -05:00
|
|
|
}
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|