2020-02-21 13:21:51 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
use deno_core::BufVec;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ErrBox;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-02-21 13:21:51 -05:00
|
|
|
use futures::future::poll_fn;
|
|
|
|
use notify::event::Event as NotifyEvent;
|
|
|
|
use notify::Error as NotifyError;
|
|
|
|
use notify::EventKind;
|
|
|
|
use notify::RecommendedWatcher;
|
|
|
|
use notify::RecursiveMode;
|
|
|
|
use notify::Watcher;
|
|
|
|
use serde::Serialize;
|
2020-09-05 20:34:02 -04:00
|
|
|
use serde_derive::Deserialize;
|
|
|
|
use serde_json::Value;
|
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;
|
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
|
|
|
super::reg_json_sync(rt, "op_fs_events_open", op_fs_events_open);
|
|
|
|
super::reg_json_async(rt, "op_fs_events_poll", op_fs_events_poll);
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct FsEventsResource {
|
|
|
|
#[allow(unused)]
|
|
|
|
watcher: RecommendedWatcher,
|
|
|
|
receiver: mpsc::Receiver<Result<FsEvent, ErrBox>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 {
|
|
|
|
kind: String,
|
|
|
|
paths: Vec<PathBuf>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
EventKind::Other => todo!(), // What's this for? Leaving it out for now.
|
|
|
|
}
|
|
|
|
.to_string();
|
|
|
|
FsEvent {
|
|
|
|
kind,
|
|
|
|
paths: e.paths,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_fs_events_open(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-02-21 13:21:51 -05:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-08-28 11:08:24 -04:00
|
|
|
) -> Result<Value, ErrBox> {
|
2020-02-21 13:21:51 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct OpenArgs {
|
|
|
|
recursive: bool,
|
|
|
|
paths: Vec<String>,
|
|
|
|
}
|
|
|
|
let args: OpenArgs = serde_json::from_value(args)?;
|
|
|
|
let (sender, receiver) = mpsc::channel::<Result<FsEvent, ErrBox>>(16);
|
|
|
|
let sender = std::sync::Mutex::new(sender);
|
|
|
|
let mut watcher: RecommendedWatcher =
|
|
|
|
Watcher::new_immediate(move |res: Result<NotifyEvent, NotifyError>| {
|
|
|
|
let res2 = res.map(FsEvent::from).map_err(ErrBox::from);
|
|
|
|
let mut sender = sender.lock().unwrap();
|
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 {
|
2020-09-10 09:57:45 -04:00
|
|
|
super::cli_state(state).check_read(&PathBuf::from(path))?;
|
2020-08-25 18:22:15 -04:00
|
|
|
watcher.watch(path, recursive_mode)?;
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
|
|
|
let resource = FsEventsResource { watcher, receiver };
|
2020-09-10 09:57:45 -04:00
|
|
|
let rid = state.resource_table.add("fsEvents", Box::new(resource));
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(rid))
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
|
|
|
|
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>>,
|
2020-02-21 13:21:51 -05:00
|
|
|
args: Value,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
|
|
|
) -> Result<Value, ErrBox> {
|
2020-02-21 13:21:51 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct PollArgs {
|
|
|
|
rid: u32,
|
|
|
|
}
|
|
|
|
let PollArgs { rid } = serde_json::from_value(args)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
poll_fn(move |cx| {
|
2020-09-10 09:57:45 -04:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let watcher = state
|
|
|
|
.resource_table
|
2020-02-21 13:21:51 -05:00
|
|
|
.get_mut::<FsEventsResource>(rid)
|
2020-08-25 18:22:15 -04:00
|
|
|
.ok_or_else(ErrBox::bad_resource_id)?;
|
2020-02-21 13:21:51 -05:00
|
|
|
watcher
|
|
|
|
.receiver
|
|
|
|
.poll_recv(cx)
|
|
|
|
.map(|maybe_result| match maybe_result {
|
|
|
|
Some(Ok(value)) => Ok(json!({ "value": value, "done": false })),
|
2020-08-25 18:22:15 -04:00
|
|
|
Some(Err(err)) => Err(err),
|
2020-02-21 13:21:51 -05:00
|
|
|
None => Ok(json!({ "done": true })),
|
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
})
|
|
|
|
.await
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|