-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathdeno_dir.rs
1501 lines (1356 loc) · 49.2 KB
/
deno_dir.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 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::deno_error::DenoError;
use crate::deno_error::ErrorKind;
use crate::deno_error::GetErrorKind;
use crate::disk_cache::DiskCache;
use crate::http_util;
use crate::msg;
use crate::progress::Progress;
use crate::tokio_util;
use deno::ErrBox;
use deno::ModuleSpecifier;
use dirs;
use futures::future::{loop_fn, Either, Loop};
use futures::Future;
use http;
use serde_json;
use std;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::result::Result;
use std::str;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::Mutex;
use url;
use url::Url;
/// Structure representing local or remote file.
///
/// In case of remote file `url` might be different than originally requested URL, if so
/// `redirect_source_url` will contain original URL and `url` will be equal to final location.
#[derive(Debug, Clone)]
pub struct SourceFile {
pub url: Url,
pub redirect_source_url: Option<Url>,
pub filename: PathBuf,
pub media_type: msg::MediaType,
pub source_code: Vec<u8>,
}
impl SourceFile {
// TODO(bartlomieju): this method should be implemented on new `CompiledSourceFile`
// trait and should be handled by "compiler pipeline"
pub fn js_source(&self) -> String {
if self.media_type == msg::MediaType::TypeScript {
panic!("TypeScript module has no JS source, did you forget to run it through compiler?");
}
// TODO: this should be done by compiler and JS module should be returned
if self.media_type == msg::MediaType::Json {
return format!(
"export default {};",
str::from_utf8(&self.source_code).unwrap()
);
}
// it's either JS or Unknown media type
str::from_utf8(&self.source_code).unwrap().to_string()
}
}
pub type SourceFileFuture =
dyn Future<Item = SourceFile, Error = ErrBox> + Send;
/// This trait implements synchronous and asynchronous methods
/// for file fetching.
///
/// Implementors might implement caching mechanism to
/// minimize number of fs ops/net fetches.
pub trait SourceFileFetcher {
fn check_if_supported_scheme(url: &Url) -> Result<(), ErrBox>;
fn fetch_source_file_async(
self: &Self,
specifier: &ModuleSpecifier,
) -> Box<SourceFileFuture>;
/// Required for TS compiler.
fn fetch_source_file(
self: &Self,
specifier: &ModuleSpecifier,
) -> Result<SourceFile, ErrBox>;
}
// TODO: this list should be implemented on SourceFileFetcher trait
const SUPPORTED_URL_SCHEMES: [&str; 3] = ["http", "https", "file"];
/// Simple struct implementing in-process caching to prevent multiple
/// fs reads/net fetches for same file.
#[derive(Clone, Default)]
pub struct SourceFileCache(Arc<Mutex<HashMap<String, SourceFile>>>);
impl SourceFileCache {
pub fn set(&self, key: String, source_file: SourceFile) {
let mut c = self.0.lock().unwrap();
c.insert(key, source_file);
}
pub fn get(&self, key: String) -> Option<SourceFile> {
let c = self.0.lock().unwrap();
match c.get(&key) {
Some(source_file) => Some(source_file.clone()),
None => None,
}
}
}
/// `DenoDir` serves as coordinator for multiple `DiskCache`s containing them
/// in single directory that can be controlled with `$DENO_DIR` env variable.
#[derive(Clone)]
// TODO(bartlomieju): try to remove `pub` from fields
pub struct DenoDir {
// Example: /Users/rld/.deno/
pub root: PathBuf,
// This is where we cache compilation outputs. Example:
// /Users/rld/.deno/gen/http/github.com/ry/blah.js
// TODO: this cache can be created using public API by TS compiler
pub gen_cache: DiskCache,
// /Users/rld/.deno/deps/http/github.com/ry/blah.ts
pub deps_cache: DiskCache,
pub progress: Progress,
source_file_cache: SourceFileCache,
use_disk_cache: bool,
no_remote_fetch: bool,
}
impl DenoDir {
// Must be called before using any function from this module.
// https://github.com/denoland/deno/blob/golang/deno_dir.go#L99-L111
pub fn new(
custom_root: Option<PathBuf>,
progress: Progress,
use_disk_cache: bool,
no_remote_fetch: bool,
) -> std::io::Result<Self> {
// Only setup once.
let home_dir = dirs::home_dir().expect("Could not get home directory.");
let fallback = home_dir.join(".deno");
// We use the OS cache dir because all files deno writes are cache files
// Once that changes we need to start using different roots if DENO_DIR
// is not set, and keep a single one if it is.
let default = dirs::cache_dir()
.map(|d| d.join("deno"))
.unwrap_or(fallback);
let root: PathBuf = custom_root.unwrap_or(default);
let gen = root.as_path().join("gen");
let gen_cache = DiskCache::new(&gen);
let deps = root.as_path().join("deps");
let deps_cache = DiskCache::new(&deps);
let deno_dir = Self {
root,
gen_cache,
deps_cache,
progress,
source_file_cache: SourceFileCache::default(),
use_disk_cache,
no_remote_fetch,
};
debug!("root {}", deno_dir.root.display());
debug!("deps {}", deno_dir.deps_cache.location.display());
debug!("gen {}", deno_dir.gen_cache.location.display());
Ok(deno_dir)
}
}
// TODO(bartlomieju): it might be a good idea to factor out this trait to separate
// struct instead of implementing it on DenoDir
impl SourceFileFetcher for DenoDir {
fn check_if_supported_scheme(url: &Url) -> Result<(), ErrBox> {
if !SUPPORTED_URL_SCHEMES.contains(&url.scheme()) {
return Err(
DenoError::new(
ErrorKind::UnsupportedFetchScheme,
format!("Unsupported scheme \"{}\" for module \"{}\". Supported schemes: {:#?}", url.scheme(), url, SUPPORTED_URL_SCHEMES),
).into()
);
}
Ok(())
}
fn fetch_source_file_async(
self: &Self,
specifier: &ModuleSpecifier,
) -> Box<SourceFileFuture> {
let module_url = specifier.as_url().to_owned();
debug!("fetch_source_file. specifier {} ", &module_url);
// Check if this file was already fetched and can be retrieved from in-process cache.
if let Some(source_file) = self.source_file_cache.get(specifier.to_string())
{
return Box::new(futures::future::ok(source_file));
}
let source_file_cache = self.source_file_cache.clone();
let specifier_ = specifier.clone();
let fut = self
.get_source_file_async(
&module_url,
self.use_disk_cache,
self.no_remote_fetch,
).then(move |result| {
let mut out = result.map_err(|err| {
if err.kind() == ErrorKind::NotFound {
// For NotFound, change the message to something better.
DenoError::new(
ErrorKind::NotFound,
format!("Cannot resolve module \"{}\"", module_url.to_string()),
).into()
} else {
err
}
})?;
// TODO: move somewhere?
if out.source_code.starts_with(b"#!") {
out.source_code = filter_shebang(out.source_code);
}
// Cache in-process for subsequent access.
source_file_cache.set(specifier_.to_string(), out.clone());
Ok(out)
});
Box::new(fut)
}
fn fetch_source_file(
self: &Self,
specifier: &ModuleSpecifier,
) -> Result<SourceFile, ErrBox> {
tokio_util::block_on(self.fetch_source_file_async(specifier))
}
}
// stuff related to SourceFileFetcher
impl DenoDir {
/// This is main method that is responsible for fetching local or remote files.
///
/// If this is a remote module, and it has not yet been cached, the resulting
/// download will be cached on disk for subsequent access.
///
/// If `use_disk_cache` is true then remote files are fetched from disk cache.
///
/// If `no_remote_fetch` is true then if remote file is not found it disk
/// cache this method will fail.
fn get_source_file_async(
self: &Self,
module_url: &Url,
use_disk_cache: bool,
no_remote_fetch: bool,
) -> impl Future<Item = SourceFile, Error = ErrBox> {
let url_scheme = module_url.scheme();
let is_local_file = url_scheme == "file";
if let Err(err) = DenoDir::check_if_supported_scheme(&module_url) {
return Either::A(futures::future::err(err));
}
// Local files are always fetched from disk bypassing cache entirely.
if is_local_file {
match self.fetch_local_file(&module_url) {
Ok(source_file) => {
return Either::A(futures::future::ok(source_file));
}
Err(err) => {
return Either::A(futures::future::err(err));
}
}
}
// We're dealing with remote file, first try local cache
if use_disk_cache {
match self.fetch_cached_remote_source(&module_url, None) {
Ok(Some(source_file)) => {
return Either::A(futures::future::ok(source_file));
}
Ok(None) => {
// there's no cached version
}
Err(err) => {
return Either::A(futures::future::err(err));
}
}
}
// If remote file wasn't found check if we can fetch it
if no_remote_fetch {
// We can't fetch remote file - bail out
return Either::A(futures::future::err(
std::io::Error::new(
std::io::ErrorKind::NotFound,
format!(
"cannot find remote file '{}' in cache",
module_url.to_string()
),
).into(),
));
}
// Fetch remote file and cache on-disk for subsequent access
// not cached/local, try remote.
let module_url_ = module_url.clone();
Either::B(self
.fetch_remote_source_async(&module_url)
// TODO: cache fetched remote source here - `fetch_remote_source` should only fetch with
// redirects, nothing more
.and_then(move |maybe_remote_source| match maybe_remote_source {
Some(output) => Ok(output),
None => Err(
std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("cannot find remote file '{}'", module_url_.to_string()),
).into(),
),
}))
}
/// Fetch local source file.
fn fetch_local_file(
self: &Self,
module_url: &Url,
) -> Result<SourceFile, ErrBox> {
let filepath = module_url.to_file_path().expect("File URL expected");
let source_code = match fs::read(filepath.clone()) {
Ok(c) => c,
Err(e) => return Err(e.into()),
};
let media_type = map_content_type(&filepath, None);
Ok(SourceFile {
url: module_url.clone(),
redirect_source_url: None,
filename: filepath,
media_type,
source_code,
})
}
/// Fetch cached remote file.
///
/// This is a recursive operation if source file has redirections.
///
/// It will keep reading <filename>.headers.json for information about redirection.
/// `module_initial_source_name` would be None on first call,
/// and becomes the name of the very first module that initiates the call
/// in subsequent recursions.
///
/// AKA if redirection occurs, module_initial_source_name is the source path
/// that user provides, and the final module_name is the resolved path
/// after following all redirections.
fn fetch_cached_remote_source(
self: &Self,
module_url: &Url,
maybe_initial_module_url: Option<Url>,
) -> Result<Option<SourceFile>, ErrBox> {
let source_code_headers = self.get_source_code_headers(&module_url);
// If source code headers says that it would redirect elsewhere,
// (meaning that the source file might not exist; only .headers.json is present)
// Abort reading attempts to the cached source file and and follow the redirect.
if let Some(redirect_to) = source_code_headers.redirect_to {
// E.g.
// module_name https://import-meta.now.sh/redirect.js
// filename /Users/kun/Library/Caches/deno/deps/https/import-meta.now.sh/redirect.js
// redirect_to https://import-meta.now.sh/sub/final1.js
// real_filename /Users/kun/Library/Caches/deno/deps/https/import-meta.now.sh/sub/final1.js
// real_module_name = https://import-meta.now.sh/sub/final1.js
let redirect_url = Url::parse(&redirect_to).expect("Should be valid URL");
let mut maybe_initial_module_url = maybe_initial_module_url;
// If this is the first redirect attempt,
// then maybe_initial_module_url should be None.
// In that case, use current module name as maybe_initial_module_url.
if maybe_initial_module_url.is_none() {
maybe_initial_module_url = Some(module_url.clone());
}
// Recurse.
return self
.fetch_cached_remote_source(&redirect_url, maybe_initial_module_url);
}
// No redirect needed or end of redirects.
// We can try read the file
let filepath = self
.deps_cache
.location
.join(self.deps_cache.get_cache_filename(&module_url));
let source_code = match fs::read(filepath.clone()) {
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
return Ok(None);
} else {
return Err(e.into());
}
}
Ok(c) => c,
};
let media_type = map_content_type(
&filepath,
source_code_headers.mime_type.as_ref().map(String::as_str),
);
Ok(Some(SourceFile {
url: module_url.clone(),
redirect_source_url: maybe_initial_module_url,
filename: filepath,
media_type,
source_code,
}))
}
/// Asynchronously fetch remote source file specified by the URL following redirects.
fn fetch_remote_source_async(
self: &Self,
module_url: &Url,
) -> impl Future<Item = Option<SourceFile>, Error = ErrBox> {
use crate::http_util::FetchOnceResult;
let download_job = self.progress.add("Download", &module_url.to_string());
// We write a special ".headers.json" file into the `.deno/deps` directory along side the
// cached file, containing just the media type and possible redirect target (both are http headers).
// If redirect target is present, the file itself if not cached.
// In future resolutions, we would instead follow this redirect target ("redirect_to").
loop_fn(
(
self.clone(),
None,
module_url.clone(),
),
|(
dir,
mut maybe_initial_module_url,
module_url,
)| {
let module_uri = url_into_uri(&module_url);
// Single pass fetch, either yields code or yields redirect.
http_util::fetch_string_once(module_uri).and_then(
move |fetch_once_result| {
match fetch_once_result {
FetchOnceResult::Redirect(uri) => {
// If redirects, update module_name and filename for next looped call.
let new_module_url = Url::parse(&uri.to_string()).expect("http::uri::Uri should be parseable as Url");
if maybe_initial_module_url.is_none() {
maybe_initial_module_url = Some(module_url);
}
// Not yet completed. Follow the redirect and loop.
Ok(Loop::Continue((
dir,
maybe_initial_module_url,
new_module_url,
)))
}
FetchOnceResult::Code(source, maybe_content_type) => {
// TODO: move caching logic outside this function
// We land on the code.
dir.save_source_code_headers(
&module_url,
maybe_content_type.clone(),
None,
).unwrap();
dir.save_source_code(
&module_url,
&source
).unwrap();
if let Some(redirect_source_url) = &maybe_initial_module_url {
dir.save_source_code_headers(
redirect_source_url,
maybe_content_type.clone(),
Some(module_url.to_string())
).unwrap()
}
let filepath = dir
.deps_cache
.location
.join(dir.deps_cache.get_cache_filename(&module_url));
let media_type = map_content_type(
&filepath,
maybe_content_type.as_ref().map(String::as_str),
);
let source_file = SourceFile {
url: module_url,
redirect_source_url: maybe_initial_module_url,
filename: filepath,
media_type,
source_code: source.as_bytes().to_owned(),
};
Ok(Loop::Break(Some(source_file)))
}
}
},
)
},
)
.then(move |r| {
// Explicit drop to keep reference alive until future completes.
drop(download_job);
r
})
}
/// Get header metadata associated with a remote file.
///
/// NOTE: chances are that the source file was downloaded due to redirects.
/// In this case, the headers file provides info about where we should go and get
/// the file that redirect eventually points to.
fn get_source_code_headers(self: &Self, url: &Url) -> SourceCodeHeaders {
let cache_key = self
.deps_cache
.get_cache_filename_with_extension(url, "headers.json");
if let Ok(bytes) = self.deps_cache.get(&cache_key) {
if let Ok(json_string) = std::str::from_utf8(&bytes) {
return SourceCodeHeaders::from_json_string(json_string.to_string());
}
}
SourceCodeHeaders::default()
}
/// Save contents of downloaded remote file in on-disk cache for subsequent access.
fn save_source_code(
self: &Self,
url: &Url,
source: &str,
) -> std::io::Result<()> {
let cache_key = self.deps_cache.get_cache_filename(url);
// May not exist. DON'T unwrap.
let _ = self.deps_cache.remove(&cache_key);
self.deps_cache.set(&cache_key, source.as_bytes())
}
/// Save headers related to source file to {filename}.headers.json file,
/// only when there is actually something necessary to save.
///
/// For example, if the extension ".js" already mean JS file and we have
/// content type of "text/javascript", then we would not save the mime type.
///
/// If nothing needs to be saved, the headers file is not created.
fn save_source_code_headers(
self: &Self,
url: &Url,
mime_type: Option<String>,
redirect_to: Option<String>,
) -> std::io::Result<()> {
let cache_key = self
.deps_cache
.get_cache_filename_with_extension(url, "headers.json");
// Remove possibly existing stale .headers.json file.
// May not exist. DON'T unwrap.
let _ = self.deps_cache.remove(&cache_key);
let headers = SourceCodeHeaders {
mime_type,
redirect_to,
};
let cache_filename = self.deps_cache.get_cache_filename(url);
if let Ok(maybe_json_string) = headers.to_json_string(&cache_filename) {
if let Some(json_string) = maybe_json_string {
return self.deps_cache.set(&cache_key, json_string.as_bytes());
}
}
Ok(())
}
}
fn map_file_extension(path: &Path) -> msg::MediaType {
match path.extension() {
None => msg::MediaType::Unknown,
Some(os_str) => match os_str.to_str() {
Some("ts") => msg::MediaType::TypeScript,
Some("js") => msg::MediaType::JavaScript,
Some("mjs") => msg::MediaType::JavaScript,
Some("json") => msg::MediaType::Json,
_ => msg::MediaType::Unknown,
},
}
}
// convert a ContentType string into a enumerated MediaType
fn map_content_type(path: &Path, content_type: Option<&str>) -> msg::MediaType {
match content_type {
Some(content_type) => {
// sometimes there is additional data after the media type in
// Content-Type so we have to do a bit of manipulation so we are only
// dealing with the actual media type
let ct_vector: Vec<&str> = content_type.split(';').collect();
let ct: &str = ct_vector.first().unwrap();
match ct.to_lowercase().as_ref() {
"application/typescript"
| "text/typescript"
| "video/vnd.dlna.mpeg-tts"
| "video/mp2t"
| "application/x-typescript" => msg::MediaType::TypeScript,
"application/javascript"
| "text/javascript"
| "application/ecmascript"
| "text/ecmascript"
| "application/x-javascript" => msg::MediaType::JavaScript,
"application/json" | "text/json" => msg::MediaType::Json,
"text/plain" => map_file_extension(path),
_ => {
debug!("unknown content type: {}", content_type);
msg::MediaType::Unknown
}
}
}
None => map_file_extension(path),
}
}
fn filter_shebang(bytes: Vec<u8>) -> Vec<u8> {
let string = str::from_utf8(&bytes).unwrap();
if let Some(i) = string.find('\n') {
let (_, rest) = string.split_at(i);
rest.as_bytes().to_owned()
} else {
Vec::new()
}
}
fn url_into_uri(url: &url::Url) -> http::uri::Uri {
http::uri::Uri::from_str(&url.to_string())
.expect("url::Url should be parseable as http::uri::Uri")
}
#[derive(Debug, Default)]
/// Header metadata associated with a particular "symbolic" source code file.
/// (the associated source code file might not be cached, while remaining
/// a user accessible entity through imports (due to redirects)).
pub struct SourceCodeHeaders {
/// MIME type of the source code.
pub mime_type: Option<String>,
/// Where should we actually look for source code.
/// This should be an absolute path!
pub redirect_to: Option<String>,
}
static MIME_TYPE: &'static str = "mime_type";
static REDIRECT_TO: &'static str = "redirect_to";
impl SourceCodeHeaders {
pub fn from_json_string(headers_string: String) -> Self {
// TODO: use serde for deserialization
let maybe_headers_json: serde_json::Result<serde_json::Value> =
serde_json::from_str(&headers_string);
if let Ok(headers_json) = maybe_headers_json {
let mime_type = headers_json[MIME_TYPE].as_str().map(String::from);
let redirect_to = headers_json[REDIRECT_TO].as_str().map(String::from);
return SourceCodeHeaders {
mime_type,
redirect_to,
};
}
SourceCodeHeaders::default()
}
// TODO: remove this nonsense `cache_filename` param, this should be
// done when instantiating SourceCodeHeaders
pub fn to_json_string(
self: &Self,
cache_filename: &Path,
) -> Result<Option<String>, serde_json::Error> {
// TODO(kevinkassimo): consider introduce serde::Deserialize to make things simpler.
// This is super ugly at this moment...
// Had trouble to make serde_derive work: I'm unable to build proc-macro2.
let mut value_map = serde_json::map::Map::new();
if let Some(mime_type) = &self.mime_type {
let resolved_mime_type =
map_content_type(Path::new(""), Some(mime_type.clone().as_str()));
// TODO: fix this
let ext_based_mime_type = map_file_extension(cache_filename);
// Add mime to headers only when content type is different from extension.
if ext_based_mime_type == msg::MediaType::Unknown
|| resolved_mime_type != ext_based_mime_type
{
value_map.insert(MIME_TYPE.to_string(), json!(mime_type));
}
}
if let Some(redirect_to) = &self.redirect_to {
value_map.insert(REDIRECT_TO.to_string(), json!(redirect_to));
}
if value_map.is_empty() {
return Ok(None);
}
serde_json::to_string(&value_map)
.and_then(|serialized| Ok(Some(serialized)))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::fs as deno_fs;
use tempfile::TempDir;
impl DenoDir {
/// Fetch remote source code.
fn fetch_remote_source(
self: &Self,
module_url: &Url,
_filepath: &Path,
) -> Result<Option<SourceFile>, ErrBox> {
tokio_util::block_on(self.fetch_remote_source_async(module_url))
}
/// Synchronous version of get_source_file_async
fn get_source_file(
self: &Self,
module_url: &Url,
use_disk_cache: bool,
no_remote_fetch: bool,
) -> Result<SourceFile, ErrBox> {
tokio_util::block_on(self.get_source_file_async(
module_url,
use_disk_cache,
no_remote_fetch,
))
}
}
fn setup_deno_dir(dir_path: &Path) -> DenoDir {
DenoDir::new(Some(dir_path.to_path_buf()), Progress::new(), true, false)
.expect("setup fail")
}
fn test_setup() -> (TempDir, DenoDir) {
let temp_dir = TempDir::new().expect("tempdir fail");
let deno_dir = setup_deno_dir(temp_dir.path());
(temp_dir, deno_dir)
}
macro_rules! file_url {
($path:expr) => {
if cfg!(target_os = "windows") {
concat!("file:///C:", $path)
} else {
concat!("file://", $path)
}
};
}
#[test]
fn test_source_code_headers_get_and_save() {
let (_temp_dir, deno_dir) = test_setup();
let url = Url::parse("http://example.com/f.js").unwrap();
let headers_filepath = deno_dir.deps_cache.location.join(
deno_dir
.deps_cache
.get_cache_filename_with_extension(&url, "headers.json"),
);
if let Some(ref parent) = headers_filepath.parent() {
fs::create_dir_all(parent).unwrap();
};
let _ = deno_fs::write_file(
headers_filepath.as_path(),
"{\"mime_type\":\"text/javascript\",\"redirect_to\":\"http://example.com/a.js\"}",
0o666
);
let headers = deno_dir.get_source_code_headers(&url);
assert_eq!(headers.mime_type.clone().unwrap(), "text/javascript");
assert_eq!(
headers.redirect_to.clone().unwrap(),
"http://example.com/a.js"
);
let _ = deno_dir.save_source_code_headers(
&url,
Some("text/typescript".to_owned()),
Some("http://deno.land/a.js".to_owned()),
);
let headers2 = deno_dir.get_source_code_headers(&url);
assert_eq!(headers2.mime_type.clone().unwrap(), "text/typescript");
assert_eq!(
headers2.redirect_to.clone().unwrap(),
"http://deno.land/a.js"
);
}
#[test]
fn test_get_source_code_1() {
let (temp_dir, deno_dir) = test_setup();
// http_util::fetch_sync_string requires tokio
tokio_util::init(|| {
let module_url =
Url::parse("http://localhost:4545/tests/subdir/mod2.ts").unwrap();
let headers_file_name = deno_dir.deps_cache.location.join(
deno_dir
.deps_cache
.get_cache_filename_with_extension(&module_url, "headers.json"),
);
let result = deno_dir.get_source_file(&module_url, true, false);
assert!(result.is_ok());
let r = result.unwrap();
assert_eq!(
r.source_code,
"export { printHello } from \"./print_hello.ts\";\n".as_bytes()
);
assert_eq!(&(r.media_type), &msg::MediaType::TypeScript);
// Should not create .headers.json file due to matching ext
assert!(fs::read_to_string(&headers_file_name).is_err());
// Modify .headers.json, write using fs write and read using save_source_code_headers
let _ =
fs::write(&headers_file_name, "{ \"mime_type\": \"text/javascript\" }");
let result2 = deno_dir.get_source_file(&module_url, true, false);
assert!(result2.is_ok());
let r2 = result2.unwrap();
assert_eq!(
r2.source_code,
"export { printHello } from \"./print_hello.ts\";\n".as_bytes()
);
// If get_source_file does not call remote, this should be JavaScript
// as we modified before! (we do not overwrite .headers.json due to no http fetch)
assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript);
assert_eq!(
deno_dir
.get_source_code_headers(&module_url)
.mime_type
.unwrap(),
"text/javascript"
);
// Modify .headers.json again, but the other way around
let _ = deno_dir.save_source_code_headers(
&module_url,
Some("application/json".to_owned()),
None,
);
let result3 = deno_dir.get_source_file(&module_url, true, false);
assert!(result3.is_ok());
let r3 = result3.unwrap();
assert_eq!(
r3.source_code,
"export { printHello } from \"./print_hello.ts\";\n".as_bytes()
);
// If get_source_file does not call remote, this should be JavaScript
// as we modified before! (we do not overwrite .headers.json due to no http fetch)
assert_eq!(&(r3.media_type), &msg::MediaType::Json);
assert!(
fs::read_to_string(&headers_file_name)
.unwrap()
.contains("application/json")
);
// let's create fresh instance of DenoDir (simulating another freshh Deno process)
// and don't use cache
let deno_dir = setup_deno_dir(temp_dir.path());
let result4 = deno_dir.get_source_file(&module_url, false, false);
assert!(result4.is_ok());
let r4 = result4.unwrap();
let expected4 =
"export { printHello } from \"./print_hello.ts\";\n".as_bytes();
assert_eq!(r4.source_code, expected4);
// Now the old .headers.json file should have gone! Resolved back to TypeScript
assert_eq!(&(r4.media_type), &msg::MediaType::TypeScript);
assert!(fs::read_to_string(&headers_file_name).is_err());
});
}
#[test]
fn test_get_source_code_2() {
let (temp_dir, deno_dir) = test_setup();
// http_util::fetch_sync_string requires tokio
tokio_util::init(|| {
let module_url =
Url::parse("http://localhost:4545/tests/subdir/mismatch_ext.ts")
.unwrap();
let headers_file_name = deno_dir.deps_cache.location.join(
deno_dir
.deps_cache
.get_cache_filename_with_extension(&module_url, "headers.json"),
);
let result = deno_dir.get_source_file(&module_url, true, false);
assert!(result.is_ok());
let r = result.unwrap();
let expected = "export const loaded = true;\n".as_bytes();
assert_eq!(r.source_code, expected);
// Mismatch ext with content type, create .headers.json
assert_eq!(&(r.media_type), &msg::MediaType::JavaScript);
assert_eq!(
deno_dir
.get_source_code_headers(&module_url)
.mime_type
.unwrap(),
"text/javascript"
);
// Modify .headers.json
let _ = deno_dir.save_source_code_headers(
&module_url,
Some("text/typescript".to_owned()),
None,
);
let result2 = deno_dir.get_source_file(&module_url, true, false);
assert!(result2.is_ok());
let r2 = result2.unwrap();
let expected2 = "export const loaded = true;\n".as_bytes();
assert_eq!(r2.source_code, expected2);
// If get_source_file does not call remote, this should be TypeScript
// as we modified before! (we do not overwrite .headers.json due to no http fetch)
assert_eq!(&(r2.media_type), &msg::MediaType::TypeScript);
assert!(fs::read_to_string(&headers_file_name).is_err());
// let's create fresh instance of DenoDir (simulating another freshh Deno process)
// and don't use cache
let deno_dir = setup_deno_dir(temp_dir.path());
let result3 = deno_dir.get_source_file(&module_url, false, false);
assert!(result3.is_ok());
let r3 = result3.unwrap();
let expected3 = "export const loaded = true;\n".as_bytes();
assert_eq!(r3.source_code, expected3);
// Now the old .headers.json file should be overwritten back to JavaScript!
// (due to http fetch)
assert_eq!(&(r3.media_type), &msg::MediaType::JavaScript);
assert_eq!(
deno_dir
.get_source_code_headers(&module_url)
.mime_type
.unwrap(),
"text/javascript"
);
});
}
#[test]
fn test_get_source_code_multiple_downloads_of_same_file() {
let (_temp_dir, deno_dir) = test_setup();
// http_util::fetch_sync_string requires tokio
tokio_util::init(|| {
let specifier = ModuleSpecifier::resolve_url(
"http://localhost:4545/tests/subdir/mismatch_ext.ts",
).unwrap();
let headers_file_name = deno_dir.deps_cache.location.join(
deno_dir.deps_cache.get_cache_filename_with_extension(
specifier.as_url(),
"headers.json",
),
);
// first download
let result = deno_dir.fetch_source_file(&specifier);
assert!(result.is_ok());
let result = fs::File::open(&headers_file_name);
assert!(result.is_ok());
let headers_file = result.unwrap();
// save modified timestamp for headers file
let headers_file_metadata = headers_file.metadata().unwrap();
let headers_file_modified = headers_file_metadata.modified().unwrap();
// download file again, it should use already fetched file even though `use_disk_cache` is set to
// false, this can be verified using source header file creation timestamp (should be
// the same as after first download)
let result = deno_dir.fetch_source_file(&specifier);
assert!(result.is_ok());
let result = fs::File::open(&headers_file_name);
assert!(result.is_ok());
let headers_file_2 = result.unwrap();
// save modified timestamp for headers file