-
Notifications
You must be signed in to change notification settings - Fork 12
/
filewatcher.rs
1049 lines (975 loc) · 44 KB
/
filewatcher.rs
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2023 Bryan A. Jones.
//
// This file is part of the CodeChat Editor. The CodeChat Editor is free
// software: you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// The CodeChat Editor is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// the CodeChat Editor. If not, see
// [http://www.gnu.org/licenses](http://www.gnu.org/licenses).
/// # `filewatcher.rs` -- Implement the File Watcher "IDE"
// ## Imports
//
// ### Standard library
use std::{
path::{Path, PathBuf},
time::Duration,
};
// ### Third-party
use actix_web::{
error::Error,
get,
http::header::{self, ContentType},
web, HttpRequest, HttpResponse, Responder,
};
use dunce::simplified;
use indoc::formatdoc;
use lazy_static::lazy_static;
use log::{error, info, warn};
use notify_debouncer_full::{
new_debouncer,
notify::{EventKind, RecursiveMode},
DebounceEventResult,
};
use regex::Regex;
use tokio::{
fs::DirEntry,
fs::{self, File},
io::AsyncReadExt,
select,
sync::mpsc,
};
use urlencoding;
#[cfg(target_os = "windows")]
use win_partitions::win_api::get_logical_drive;
// ### Local
use super::{
client_websocket, escape_html, get_client_framework, get_connection_id, html_not_found,
html_wrapper, path_display, send_response, AppState, EditorMessage, EditorMessageContents,
UpdateMessageContents, WebsocketQueues,
};
use crate::{
oneshot_send,
processing::{
codechat_for_web_to_source, source_to_codechat_for_web_string, TranslationResultsString,
},
queue_send,
webserver::{
filesystem_endpoint, get_test_mode, make_simple_http_response, path_to_url, url_to_path,
ResultOkTypes,
},
};
// ## Globals
lazy_static! {
/// Matches a bare drive letter. Only needed on Windows.
static ref DRIVE_LETTER_REGEX: Regex = Regex::new("^[a-zA-Z]:$").unwrap();
}
pub const FILEWATCHER_PATH_PREFIX: &[&str] = &["fw", "fsc"];
/// ## File browser endpoints
///
/// The file browser provides a very crude interface, allowing a user to select
/// a file from the local filesystem for editing. Long term, this should be
/// replaced by something better.
///
/// Redirect from the root of the filesystem to the actual root path on this OS.
pub async fn filewatcher_root_fs_redirect() -> impl Responder {
HttpResponse::TemporaryRedirect()
.insert_header((header::LOCATION, "/fw/fsb/"))
.finish()
}
/// Dispatch to support functions which serve either a directory listing, a
/// CodeChat Editor file, or a normal file.
///
/// `fsb` stands for "FileSystem Browser" -- directories provide a simple
/// navigation GUI; files load the Client framework.
///
/// Omit code coverage -- this is a temporary interface, until IDE integration
/// replaces this.
#[cfg(not(tarpaulin_include))]
#[get("/fw/fsb/{path:.*}")]
async fn filewatcher_browser_endpoint(
req: HttpRequest,
app_state: web::Data<AppState>,
orig_path: web::Path<String>,
) -> impl Responder {
let mut fixed_path = orig_path.to_string();
#[cfg(target_os = "windows")]
// On Windows, a path of `drive_letter:` needs a `/` appended.
if DRIVE_LETTER_REGEX.is_match(&fixed_path) {
fixed_path += "/";
} else if fixed_path.is_empty() {
// If there's no drive letter yet, we will always use `dir_listing` to
// select a drive.
return dir_listing("", Path::new("")).await;
}
// All other cases (for example, `C:\a\path\to\file.txt`) are OK.
// For Linux/OS X, prepend a slash, so that `a/path/to/file.txt` becomes
// `/a/path/to/file.txt`.
#[cfg(not(target_os = "windows"))]
let fixed_path = "/".to_string() + &fixed_path;
// Handle any
// [errors](https://doc.rust-lang.org/std/fs/fn.canonicalize.html#errors).
let canon_path = match Path::new(&fixed_path).canonicalize() {
Ok(p) => p,
Err(err) => {
return html_not_found(&format!(
"<p>The requested path <code>{fixed_path}</code> is not valid: {err}.</p>"
))
}
};
if canon_path.is_dir() {
return dir_listing(orig_path.as_str(), &canon_path).await;
} else if canon_path.is_file() {
// Get an ID for this connection.
let connection_id = get_connection_id(&app_state);
actix_rt::spawn(async move {
processing_task(&canon_path, app_state, connection_id).await;
});
return match get_client_framework(get_test_mode(&req), "fw/ws", &connection_id.to_string())
{
Ok(s) => HttpResponse::Ok().content_type(ContentType::html()).body(s),
Err(err) => html_not_found(&format!("<p>{}</p>", escape_html(&err))),
};
}
// It's not a directory or a file...we give up. For simplicity, don't handle
// symbolic links.
html_not_found(&format!(
"<p>The requested path <code>{}</code> is not a directory or a file.</p>",
path_display(&canon_path)
))
}
/// ### Directory browser
///
/// Create a web page listing all files and subdirectories of the provided
/// directory.
///
/// Omit code coverage -- this is a temporary interface, until IDE integration
/// replaces this.
#[cfg(not(tarpaulin_include))]
async fn dir_listing(web_path: &str, dir_path: &Path) -> HttpResponse {
// Special case on Windows: list drive letters.
#[cfg(target_os = "windows")]
if dir_path == Path::new("") {
// List drive letters in Windows
let mut drive_html = String::new();
let logical_drives = match get_logical_drive() {
Ok(v) => v,
Err(err) => return html_not_found(&format!("Unable to list drive letters: {}.", err)),
};
for drive_letter in logical_drives {
drive_html.push_str(&format!(
"<li><a href='/fw/fsb/{drive_letter}:/'>{drive_letter}:</a></li>\n"
));
}
return HttpResponse::Ok()
.content_type(ContentType::html())
.body(html_wrapper(&formatdoc!(
"
<h1>Drives</h1>
<ul>
{drive_html}
</ul>
"
)));
}
// List each file/directory with appropriate links.
let mut unwrapped_read_dir = match fs::read_dir(dir_path).await {
Ok(p) => p,
Err(err) => {
return html_not_found(&format!(
"<p>Unable to list the directory {}: {err}/</p>",
path_display(dir_path)
))
}
};
// Get a listing of all files and directories
let mut files: Vec<DirEntry> = Vec::new();
let mut dirs: Vec<DirEntry> = Vec::new();
loop {
match unwrapped_read_dir.next_entry().await {
Ok(v) => {
if let Some(dir_entry) = v {
let file_type = match dir_entry.file_type().await {
Ok(x) => x,
Err(err) => {
return html_not_found(&format!(
"<p>Unable to determine the type of {}: {err}.",
path_display(&dir_entry.path()),
))
}
};
if file_type.is_file() {
files.push(dir_entry);
} else {
// Group symlinks with dirs.
dirs.push(dir_entry);
}
} else {
break;
}
}
Err(err) => {
return html_not_found(&format!("<p>Unable to read file in directory: {err}."))
}
};
}
// Sort them -- case-insensitive on Windows, normally on Linux/OS X.
#[cfg(target_os = "windows")]
let file_name_key = |a: &DirEntry| {
Ok::<String, std::ffi::OsString>(a.file_name().into_string()?.to_lowercase())
};
#[cfg(not(target_os = "windows"))]
let file_name_key =
|a: &DirEntry| Ok::<String, std::ffi::OsString>(a.file_name().into_string()?);
files.sort_unstable_by_key(file_name_key);
dirs.sort_unstable_by_key(file_name_key);
// Put this on the resulting webpage. List directories first.
let mut dir_html = String::new();
for dir in dirs {
let dir_name = match dir.file_name().into_string() {
Ok(v) => v,
Err(err) => {
return html_not_found(&format!(
"<p>Unable to decode directory name '{err:?}' as UTF-8."
))
}
};
let encoded_dir = urlencoding::encode(&dir_name);
dir_html += &format!(
"<li><a href='/fw/fsb/{web_path}{}{encoded_dir}'>{dir_name}</a></li>\n",
// If this is a raw drive letter, then the path already ends with a
// slash, such as `C:/`. Don't add a second slash in this case.
// Otherwise, add a slash to make `C:/foo` into `C:/foo/`.
//
// Likewise, the Linux root path of `/` already ends with a slash,
// while all other paths such a `/foo` don't. To detect this, look
// for an empty `web_path`.
if web_path.ends_with('/') || web_path.is_empty() {
""
} else {
"/"
}
);
}
// List files second.
let mut file_html = String::new();
for file in files {
let file_name = match file.file_name().into_string() {
Ok(v) => v,
Err(err) => {
return html_not_found(&format!("<p>Unable to decode file name {err:?} as UTF-8.",))
}
};
let encoded_file = urlencoding::encode(&file_name);
file_html += &formatdoc!(
r#"
<li><a href="/fw/fsb/{web_path}/{encoded_file}" target="_blank">{file_name}</a></li>
"#
);
}
let body = formatdoc!(
"
<h1>Directory {}</h1>
<h2>Subdirectories</h2>
<ul>
{dir_html}
</ul>
<h2>Files</h2>
<ul>
{file_html}
</ul>
",
path_display(dir_path)
);
HttpResponse::Ok()
.content_type(ContentType::html())
.body(html_wrapper(&body))
}
/// `fsc` stands for "FileSystem Client", and provides the Client contents from
/// the filesystem.
#[get("/fw/fsc/{connection_id}/{file_path:.*}")]
async fn filewatcher_client_endpoint(
request_path: web::Path<(String, String)>,
req: HttpRequest,
app_state: web::Data<AppState>,
) -> HttpResponse {
filesystem_endpoint(request_path, &req, &app_state).await
}
async fn processing_task(file_path: &Path, app_state: web::Data<AppState>, connection_id: u32) {
// #### Filewatcher IDE
//
// This is a CodeChat Editor file. Start up the Filewatcher IDE tasks:
//
// 1. A task to watch for changes to the file, notifying the CodeChat
// Editor Client when the file should be reloaded.
// 2. A task to receive and respond to messages from the CodeChat Editor
// Client.
//
// First, allocate variables needed by these two tasks.
//
// The path to the currently open CodeChat Editor file.
let Ok(mut current_filepath) = file_path.to_path_buf().canonicalize() else {
error!("Unable to canonicalize path {file_path:?}.");
return;
};
current_filepath = PathBuf::from(simplified(¤t_filepath));
let Some(current_filepath_str) = current_filepath.to_str() else {
error!("Unable to convert path {current_filepath:?} to string.");
return;
};
let mut current_filepath_str = current_filepath_str.to_string();
// #### The filewatcher task.
actix_rt::spawn(async move {
'task: {
// Use a channel to send from the watcher (which runs in another
// thread) into this async (task) context.
let (watcher_tx, mut watcher_rx) = mpsc::channel(10);
// Watch this file. Use the debouncer, to avoid multiple
// notifications for the same file. This approach returns a result
// of either a working debouncer or any errors that occurred. The
// debouncer's scope needs live as long as this connection does;
// dropping it early means losing file change notifications.
let Ok(mut debounced_watcher) = new_debouncer(
Duration::from_secs(2),
None,
// Note that this runs in a separate thread created by the
// watcher, not in an async context. Therefore, use a blocking
// send.
move |result: DebounceEventResult| {
if let Err(err) = watcher_tx.blocking_send(result) {
// Note: we can't break here, since this runs in a
// separate thread. We have no way to shut down the task
// (which would be the best action to take.)
error!("Unable to send: {err}");
}
},
) else {
error!("Unable to create debouncer.");
break 'task;
};
if let Err(err) =
debounced_watcher.watch(¤t_filepath, RecursiveMode::NonRecursive)
{
error!("Unable to watch file: {err}");
break 'task;
};
// Create the queues for the websocket connection to communicate
// with this task.
let (from_websocket_tx, mut from_websocket_rx) = mpsc::channel(10);
let (to_websocket_tx, to_websocket_rx) = mpsc::channel(10);
app_state.filewatcher_client_queues.lock().unwrap().insert(
connection_id.to_string(),
WebsocketQueues {
from_websocket_tx,
to_websocket_rx,
},
);
// Provide it a file to open.
let url_pathbuf = path_to_url("/fw/fsc", &connection_id.to_string(), ¤t_filepath);
let mut id: f64 = 0.0;
queue_send!(to_websocket_tx.send(EditorMessage {
id,
message: EditorMessageContents::CurrentFile(url_pathbuf)
}), 'task);
// Note: it's OK to postpone the increment to here; if the
// `queue_send` exits before this runs, the message didn't get sent,
// so the ID wasn't used.
id += 1.0;
// Create a queue for HTTP requests fo communicate with this task.
let (from_http_tx, mut from_http_rx) = mpsc::channel(10);
app_state
.processing_task_queue_tx
.lock()
.unwrap()
.insert(connection_id.to_string(), from_http_tx);
loop {
select! {
// Process results produced by the file watcher.
Some(result) = watcher_rx.recv() => {
match result {
Err(err_vec) => {
for err in err_vec {
// Report errors locally and to the CodeChat
// Editor.
let msg = format!("Watcher error: {err}");
error!("{msg}");
// Send using ID 0 to indicate this isn't a
// response to a message received from the
// client.
send_response(&to_websocket_tx, 0.0, Err(msg)).await;
}
}
Ok(debounced_event_vec) => {
for debounced_event in debounced_event_vec {
match debounced_event.event.kind {
EventKind::Modify(_modify_kind) => {
// On Windows, the `_modify_kind` is `Any`;
// therefore; ignore it rather than trying
// to look at only content modifications.
if debounced_event.event.paths.len() == 1 && debounced_event.event.paths[0] == current_filepath {
// Since the parents are identical, send an
// update. First, read the modified file.
let mut file_contents = String::new();
let read_ret = match File::open(¤t_filepath).await {
Ok(fc) => fc,
Err(_err) => {
// We can't open the file -- it's been
// moved or deleted. Close the file.
queue_send!(to_websocket_tx.send(EditorMessage {
id,
message: EditorMessageContents::Closed
}));
id += 1.0;
continue;
}
}
.read_to_string(&mut file_contents)
.await;
// Close the file if it can't be read as
// Unicode text.
if read_ret.is_err() {
queue_send!(to_websocket_tx.send(EditorMessage {
id,
message: EditorMessageContents::Closed
}));
id += 1.0;
}
// Translate the file.
let (translation_results_string, _path_to_toc) =
source_to_codechat_for_web_string(&file_contents, ¤t_filepath, false);
if let TranslationResultsString::CodeChat(cc) = translation_results_string {
// Send the new contents
queue_send!(to_websocket_tx.send(EditorMessage {
id,
message: EditorMessageContents::Update(UpdateMessageContents {
file_path: current_filepath_str.clone(),
contents: Some(cc),
cursor_position: None,
scroll_position: None,
}),
}));
id += 1.0;
} else {
// Close the file -- it's not CodeChat
// anymore.
queue_send!(to_websocket_tx.send(EditorMessage {
id,
message: EditorMessageContents::Closed
}));
id += 1.0;
}
} else {
warn!("TODO: Modification to different file.")
}
}
_ => {
// TODO: handle delete.
info!("Watcher event: {debounced_event:?}.");
}
}
}
}
}
}
Some(http_request) = from_http_rx.recv() => {
let (simple_http_response, option_update) = make_simple_http_response(&http_request, ¤t_filepath).await;
if let Some(update) = option_update {
// Send the update to the client.
queue_send!(to_websocket_tx.send(EditorMessage { id, message: update }));
id += 1.0;
}
oneshot_send!(http_request.response_queue.send(simple_http_response));
}
Some(m) = from_websocket_rx.recv() => {
match m.message {
EditorMessageContents::Update(update_message_contents) => {
let result = 'process: {
// Check that the file path matches the current file. If `canonicalize` fails, then the files don't match.
if update_message_contents.file_path != current_filepath_str {
break 'process Err(format!(
"Update for file '{}' doesn't match current file '{current_filepath_str}'.", update_message_contents.file_path
));
}
// With code or a path, there's nothing to
// save.
let codechat_for_web = match update_message_contents.contents {
None => break 'process Ok(ResultOkTypes::Void),
Some(cfw) => cfw,
};
// Translate from the CodeChatForWeb format
// to the contents of a source file.
let file_contents = match codechat_for_web_to_source(
&codechat_for_web,
) {
Ok(r) => r,
Err(message) => {
break 'process Err(format!(
"Unable to translate to source: {message}"
));
}
};
if let Err(err) = debounced_watcher.unwatch(¤t_filepath) {
let msg = format!(
"Unable to unwatch file '{}': {err}.",
current_filepath.to_string_lossy()
);
break 'process Err(msg);
}
// Save this string to a file.
if let Err(err) = fs::write(current_filepath.as_path(), file_contents).await {
let msg = format!(
"Unable to save file '{}': {err}.",
current_filepath.to_string_lossy()
);
break 'process Err(msg);
}
if let Err(err) = debounced_watcher.watch(¤t_filepath, RecursiveMode::NonRecursive) {
let msg = format!(
"Unable to watch file '{}': {err}.",
current_filepath.to_string_lossy()
);
break 'process Err(msg);
}
Ok(ResultOkTypes::Void)
};
send_response(&to_websocket_tx, m.id, result).await;
}
EditorMessageContents::CurrentFile(url_string) => {
let result = match url_to_path(&url_string, FILEWATCHER_PATH_PREFIX) {
Err(err) => Err(err),
Ok(file_path) => 'err_exit: {
// We finally have the desired path! First,
// unwatch the old path.
if let Err(err) = debounced_watcher.unwatch(¤t_filepath) {
break 'err_exit Err(format!(
"Unable to unwatch file '{}': {err}.",
current_filepath.to_string_lossy()
));
}
// Update to the new path.
current_filepath = file_path;
current_filepath_str = match current_filepath.to_str() {
Some(v) => v.to_string(),
None => break 'err_exit Err(format!("Unable to convert path {current_filepath:?} to string."))
};
// Watch the new file.
if let Err(err) = debounced_watcher.watch(¤t_filepath, RecursiveMode::NonRecursive) {
break 'err_exit Err(format!(
"Unable to watch file '{}': {err}.",
current_filepath.to_string_lossy()
));
}
// Indicate there was no error in the
// `Result` message.
Ok(ResultOkTypes::Void)
}
};
send_response(&to_websocket_tx, m.id, result).await;
},
// Process a result, the respond to a message we
// sent.
EditorMessageContents::Result(message_result) => {
// Report errors to the log.
if let Err(err) = message_result {
error!("Error in message {}: {err}", m.id);
}
}
EditorMessageContents::Closed => {
info!("Filewatcher closing");
break;
}
EditorMessageContents::Opened(_) | EditorMessageContents::ClientHtml(_) | EditorMessageContents::RequestClose => {
let msg = format!("Client sent unsupported message type {m:?}");
error!("{msg}");
send_response(&to_websocket_tx, m.id, Err(msg)).await;
}
other => {
warn!("Unhandled message {other:?}");
}
}
}
else => break
}
}
from_websocket_rx.close();
if app_state
.processing_task_queue_tx
.lock()
.unwrap()
.remove(&connection_id.to_string())
.is_none()
{
error!(
"Unable to remove connection ID {connection_id} from processing task queues."
);
}
// Drain any remaining messages after closing the queue.
while let Some(m) = from_websocket_rx.recv().await {
warn!("Dropped queued message {m:?}");
}
}
info!("Watcher closed.");
});
}
/// Define a websocket handler for the CodeChat Editor Client.
#[get("/fw/ws/{connection_id}")]
pub async fn filewatcher_websocket(
connection_id: web::Path<String>,
req: HttpRequest,
body: web::Payload,
app_state: web::Data<AppState>,
) -> Result<HttpResponse, Error> {
client_websocket(
connection_id,
req,
body,
app_state.filewatcher_client_queues.clone(),
)
.await
}
// ## Tests
#[cfg(test)]
mod tests {
use std::{
fs,
path::{Path, PathBuf},
str::FromStr,
time::Duration,
};
use actix_http::Request;
use actix_web::{
body::BoxBody,
dev::{Service, ServiceResponse},
test, web, App,
};
use assertables::assert_starts_with;
use dunce::simplified;
use path_slash::PathExt;
use tokio::{select, sync::mpsc::Receiver, time::sleep};
use url::Url;
use super::{
super::{configure_app, make_app_data, WebsocketQueues},
send_response, AppState, EditorMessage, EditorMessageContents, UpdateMessageContents,
};
use crate::{
cast, prep_test_dir,
processing::{
source_to_codechat_for_web, CodeChatForWeb, CodeMirror, SourceFileMetadata,
TranslationResults,
},
test_utils::{check_logger_errors, configure_testing_logger},
webserver::{tests::IP_PORT, IdeType, ResultOkTypes},
};
async fn get_websocket_queues(
// A path to the temporary directory where the source file is located.
test_dir: &Path,
) -> (
WebsocketQueues,
impl Service<Request, Response = ServiceResponse<BoxBody>, Error = actix_web::Error>,
) {
let app_data = make_app_data(IP_PORT);
let app = test::init_service(configure_app(App::new(), &app_data)).await;
// Load in a test source file to create a websocket.
let uri = format!("/fw/fsb/{}/test.py", test_dir.to_string_lossy());
let req = test::TestRequest::get().uri(&uri).to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_success());
// Even after the webpage is served, the websocket task hasn't started.
// Wait a bit for that.
sleep(Duration::from_millis(10)).await;
// The web page has been served; fake the connected websocket by getting
// the appropriate tx/rx queues.
let app_state = resp.request().app_data::<web::Data<AppState>>().unwrap();
let mut joint_editors = app_state.filewatcher_client_queues.lock().unwrap();
let connection_id = *app_state.connection_id.lock().unwrap();
assert_eq!(joint_editors.len(), 1);
(
joint_editors.remove(&connection_id.to_string()).unwrap(),
app,
)
}
async fn get_message(client_rx: &mut Receiver<EditorMessage>) -> EditorMessage {
select! {
data = client_rx.recv() => {
let m = data.unwrap();
// For debugging, print out each message.
println!("{} - {:?}", m.id, m.message);
m
}
_ = sleep(Duration::from_secs(3)) => panic!("Timeout waiting for message")
}
}
macro_rules! get_message_as {
($client_rx: expr, $cast_type: ty) => {{
let m = get_message(&mut $client_rx).await;
(m.id, cast!(m.message, $cast_type))
}};
}
#[actix_web::test]
async fn test_websocket_opened_1() {
configure_testing_logger();
let (temp_dir, test_dir) = prep_test_dir!();
let (je, app) = get_websocket_queues(&test_dir).await;
let ide_tx_queue = je.from_websocket_tx;
let mut client_rx = je.to_websocket_rx;
// The initial web request for the Client framework produces a
// `CurrentFile`.
let (id, url_string) = get_message_as!(client_rx, EditorMessageContents::CurrentFile);
assert_eq!(id, 0.0);
// Compute the path this message should contain.
let mut test_path = test_dir.clone();
test_path.push("test.py");
// The comparison below fails without this.
let test_path = test_path.canonicalize().unwrap();
// The URL parser requires a valid origin.
let url = Url::parse(&format!("http://foo.com{url_string}")).unwrap();
let url_segs: Vec<_> = url
.path_segments()
.unwrap()
.map(|s| urlencoding::decode(s).unwrap())
.collect();
let url_path = PathBuf::from_str(&url_segs[3..].join("/"))
.unwrap()
.canonicalize()
.unwrap();
assert_eq!(url_path, test_path);
send_response(&ide_tx_queue, id, Ok(ResultOkTypes::Void)).await;
// 2. After fetching the file, we should get an update.
let uri = format!("/fw/fsc/1/{}/test.py", test_dir.to_string_lossy());
let req = test::TestRequest::get().uri(&uri).to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_success());
let (id, umc) = get_message_as!(client_rx, EditorMessageContents::Update);
assert_eq!(id, 1.0);
send_response(&ide_tx_queue, id, Ok(ResultOkTypes::Void)).await;
// Check the contents.
let translation_results = source_to_codechat_for_web("", &"py".to_string(), false, false);
let codechat_for_web = cast!(translation_results, TranslationResults::CodeChat);
assert_eq!(umc.contents, Some(codechat_for_web));
// Report any errors produced when removing the temporary directory.
check_logger_errors(0);
temp_dir.close().unwrap();
}
#[actix_web::test]
async fn test_websocket_update_1() {
configure_testing_logger();
let (temp_dir, test_dir) = prep_test_dir!();
let (je, app) = get_websocket_queues(&test_dir).await;
let ide_tx_queue = je.from_websocket_tx;
let mut client_rx = je.to_websocket_rx;
// The initial web request for the Client framework produces a
// `CurrentFile`.
let (id, _) = get_message_as!(client_rx, EditorMessageContents::CurrentFile);
assert_eq!(id, 0.0);
send_response(&ide_tx_queue, 0.0, Ok(ResultOkTypes::Void)).await;
// The follow-up web request for the file produces an `Update`.
let mut file_path = test_dir.clone();
file_path.push("test.py");
let file_path = simplified(&file_path.canonicalize().unwrap())
.to_str()
.unwrap()
.to_string();
let uri = format!("/fw/fsc/1/{}/test.py", test_dir.to_string_lossy());
let req = test::TestRequest::get().uri(&uri).to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_success());
let (id, _) = get_message_as!(client_rx, EditorMessageContents::Update);
assert_eq!(id, 1.0);
send_response(&ide_tx_queue, 1.0, Ok(ResultOkTypes::Void)).await;
// 1. Send an update message with no contents.
ide_tx_queue
.send(EditorMessage {
id: 0.0,
message: EditorMessageContents::Update(UpdateMessageContents {
file_path: file_path.clone(),
contents: None,
cursor_position: None,
scroll_position: None,
}),
})
.await
.unwrap();
// Check that it produces no error.
assert_eq!(
get_message_as!(client_rx, EditorMessageContents::Result),
(0.0, Ok(ResultOkTypes::Void))
);
// 2. Send invalid messages.
for (id, msg) in [
(1.0, EditorMessageContents::Opened(IdeType::VSCode(true))),
(2.0, EditorMessageContents::ClientHtml("".to_string())),
(3.0, EditorMessageContents::RequestClose),
] {
ide_tx_queue
.send(EditorMessage { id, message: msg })
.await
.unwrap();
let (id_rx, msg_rx) = get_message_as!(client_rx, EditorMessageContents::Result);
assert_eq!(id, id_rx);
assert_starts_with!(cast!(&msg_rx, Err), "Client sent unsupported message type");
}
// 3. Send an update message with no path.
ide_tx_queue
.send(EditorMessage {
id: 4.0,
message: EditorMessageContents::Update(UpdateMessageContents {
file_path: "".to_string(),
contents: Some(CodeChatForWeb {
metadata: SourceFileMetadata {
mode: "".to_string(),
},
source: CodeMirror {
doc: "".to_string(),
doc_blocks: vec![],
},
}),
cursor_position: None,
scroll_position: None,
}),
})
.await
.unwrap();
// Check that it produces an error.
let (id, err_msg) = get_message_as!(client_rx, EditorMessageContents::Result);
assert_eq!(id, 4.0);
assert_starts_with!(
cast!(err_msg, Err),
"Update for file '' doesn't match current file"
);
// 4. Send an update message with unknown source language.
ide_tx_queue
.send(EditorMessage {
id: 5.0,
message: EditorMessageContents::Update(UpdateMessageContents {
file_path: file_path.clone(),
contents: Some(CodeChatForWeb {
metadata: SourceFileMetadata {
mode: "nope".to_string(),
},
source: CodeMirror {
doc: "testing".to_string(),
doc_blocks: vec![],
},
}),
cursor_position: None,
scroll_position: None,
}),
})
.await
.unwrap();
// Check that it produces an error.
assert_eq!(
get_message_as!(client_rx, EditorMessageContents::Result),
(
5.0,
Err("Unable to translate to source: Invalid mode".to_string())
)
);
// 5. Send a valid message.
ide_tx_queue
.send(EditorMessage {
id: 6.0,
message: EditorMessageContents::Update(UpdateMessageContents {
file_path: file_path.clone(),
contents: Some(CodeChatForWeb {
metadata: SourceFileMetadata {
mode: "python".to_string(),
},
source: CodeMirror {
doc: "testing()".to_string(),
doc_blocks: vec![],
},
}),
cursor_position: None,
scroll_position: None,
}),
})
.await
.unwrap();
assert_eq!(
get_message_as!(client_rx, EditorMessageContents::Result),
(6.0, Ok(ResultOkTypes::Void))
);
// Check that the requested file is written.
let mut s = fs::read_to_string(&file_path).unwrap();
assert_eq!(s, "testing()");
// Wait for the filewatcher to debounce this file write.
sleep(Duration::from_secs(1)).await;
// 6. Change this file and verify that this produces an update.
s.push_str("123");
fs::write(&file_path, s).unwrap();
assert_eq!(
get_message_as!(client_rx, EditorMessageContents::Update),
(
2.0,
UpdateMessageContents {
file_path: file_path.clone(),
contents: Some(CodeChatForWeb {
metadata: SourceFileMetadata {
mode: "python".to_string(),
},
source: CodeMirror {
doc: "testing()123".to_string(),
doc_blocks: vec![],
},
}),
cursor_position: None,
scroll_position: None,
}
)
);
// Acknowledge this message.
send_response(&ide_tx_queue, 2.0, Ok(ResultOkTypes::Void)).await;
// 7. Rename it and check for an close (the file watcher can't detect
// the destination file, so it's treated as the file is deleted).