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
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::colors;
use crate::global_state::GlobalState;
use crate::module_graph::{ModuleGraph, ModuleGraphFile, ModuleGraphLoader};
use crate::ModuleSpecifier;
use crate::Permissions;
use deno_core::error::AnyError;
use serde::ser::Serializer;
use serde::Serialize;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::Arc;
// TODO(bartlomieju): rename
/// Struct containing a module's dependency information.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ModuleDepInfo {
local: String,
file_type: String,
compiled: Option<String>,
map: Option<String>,
dep_count: usize,
#[serde(skip_serializing)]
deps: FileInfoDepTree,
total_size: Option<usize>,
files: FileInfoDepFlatGraph,
}
impl ModuleDepInfo {
/// Creates a new `ModuleDepInfo` struct for the module with the provided `ModuleSpecifier`.
pub async fn new(
global_state: &Arc<GlobalState>,
module_specifier: ModuleSpecifier,
) -> Result<Self, AnyError> {
// First load module as if it was to be executed by worker
// including compilation step
let mut module_graph_loader = ModuleGraphLoader::new(
global_state.file_fetcher.clone(),
global_state.maybe_import_map.clone(),
Permissions::allow_all(),
false,
true,
);
module_graph_loader
.add_to_graph(&module_specifier, None)
.await?;
let module_graph = module_graph_loader.get_graph();
let ts_compiler = &global_state.ts_compiler;
let file_fetcher = &global_state.file_fetcher;
let out = file_fetcher
.fetch_cached_source_file(&module_specifier, Permissions::allow_all())
.expect("Source file should already be cached");
let local_filename = out.filename.to_string_lossy().to_string();
let compiled_filename = ts_compiler
.get_compiled_source_file(&out.url)
.ok()
.map(|file| file.filename.to_string_lossy().to_string());
let map_filename = ts_compiler
.get_source_map_file(&module_specifier)
.ok()
.map(|file| file.filename.to_string_lossy().to_string());
let file_type =
crate::media_type::enum_name_media_type(out.media_type).to_string();
let deps = FileInfoDepTree::new(&module_graph, &module_specifier);
let total_size = deps.total_size;
let dep_count = get_unique_dep_count(&module_graph) - 1;
let files = FileInfoDepFlatGraph::new(&module_graph);
let info = Self {
local: local_filename,
file_type,
compiled: compiled_filename,
map: map_filename,
dep_count,
deps,
total_size,
files,
};
Ok(info)
}
}
/// Counts the number of dependencies in the graph.
///
/// We are counting only the dependencies that are not http redirects to other files.
fn get_unique_dep_count(graph: &ModuleGraph) -> usize {
graph.iter().fold(
0,
|acc, e| {
if e.1.redirect.is_none() {
acc + 1
} else {
acc
}
},
)
}
impl std::fmt::Display for ModuleDepInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{} {}\n", colors::bold("local:"), self.local))?;
f.write_fmt(format_args!(
"{} {}\n",
colors::bold("type:"),
self.file_type
))?;
if let Some(ref compiled) = self.compiled {
f.write_fmt(format_args!(
"{} {}\n",
colors::bold("compiled:"),
compiled
))?;
}
if let Some(ref map) = self.map {
f.write_fmt(format_args!("{} {}\n", colors::bold("map:"), map))?;
}
f.write_fmt(format_args!(
"{} {} unique {}\n",
colors::bold("deps:"),
self.dep_count,
colors::gray(&format!(
"(total {})",
human_size(self.deps.total_size.unwrap_or(0) as f64),
))
))?;
f.write_fmt(format_args!(
"{} {}\n",
self.deps.name,
colors::gray(&format!("({})", human_size(self.deps.size as f64)))
))?;
for (idx, dep) in self.deps.deps.iter().enumerate() {
print_file_dep_info(&dep, "", idx == self.deps.deps.len() - 1, f)?;
}
Ok(())
}
}
/// A dependency tree of the basic module information.
///
/// Constructed from a `ModuleGraph` and `ModuleSpecifier` that
/// acts as the root of the tree.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct FileInfoDepTree {
name: String,
size: usize,
total_size: Option<usize>,
deps: Vec<FileInfoDepTree>,
}
impl FileInfoDepTree {
/// Create a `FileInfoDepTree` tree from a `ModuleGraph` and the root `ModuleSpecifier`.
pub fn new(
module_graph: &ModuleGraph,
root_specifier: &ModuleSpecifier,
) -> Self {
let mut seen = HashSet::new();
let mut total_sizes = HashMap::new();
Self::visit_module(
&mut seen,
&mut total_sizes,
module_graph,
root_specifier,
)
}
/// Visit modules recursively.
///
/// If currently visited module has not yet been seen it will be annotated with dependencies
/// and cumulative size of those deps.
fn visit_module(
seen: &mut HashSet<String>,
total_sizes: &mut HashMap<String, usize>,
graph: &ModuleGraph,
specifier: &ModuleSpecifier,
) -> Self {
let name = specifier.to_string();
let never_seen = seen.insert(name.clone());
let file = get_resolved_file(&graph, &specifier);
let size = file.size();
let mut deps = vec![];
let mut total_size = None;
if never_seen {
let mut seen_deps = HashSet::new();
deps = file
.imports
.iter()
.map(|import| &import.resolved_specifier)
.filter(|module_specifier| {
seen_deps.insert(module_specifier.as_str().to_string())
})
.map(|specifier| {
Self::visit_module(seen, total_sizes, graph, specifier)
})
.collect::<Vec<_>>();
total_size = if let Some(total_size) = total_sizes.get(&name) {
Some(total_size.to_owned())
} else {
let total: usize = deps
.iter()
.map(|dep| {
if let Some(total_size) = dep.total_size {
total_size
} else {
0
}
})
.sum();
let total = size + total;
total_sizes.insert(name.clone(), total);
Some(total)
};
}
Self {
name,
size,
total_size,
deps,
}
}
}
/// Flat graph vertex with all shallow dependencies
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct FileInfoVertex {
size: usize,
deps: Vec<String>,
}
impl FileInfoVertex {
/// Creates new `FileInfoVertex` that is a single vertex dependency module
fn new(size: usize, deps: Vec<String>) -> Self {
Self { size, deps }
}
}
struct FileInfoDepFlatGraph(HashMap<String, FileInfoVertex>);
impl FileInfoDepFlatGraph {
/// Creates new `FileInfoDepFlatGraph`, flat graph of a shallow module dependencies
///
/// Each graph vertex represents unique dependency with its all shallow dependencies
fn new(module_graph: &ModuleGraph) -> Self {
let mut inner = HashMap::new();
module_graph
.iter()
.for_each(|(module_name, module_graph_file)| {
let size = module_graph_file.size();
let mut deps = Vec::new();
module_graph_file.imports.iter().for_each(|import| {
deps.push(import.resolved_specifier.to_string());
});
inner.insert(module_name.clone(), FileInfoVertex::new(size, deps));
});
Self(inner)
}
}
impl Serialize for FileInfoDepFlatGraph {
/// Serializes inner hash map which is ordered by the key
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let ordered: BTreeMap<_, _> = self.0.iter().collect();
ordered.serialize(serializer)
}
}
/// Returns a `ModuleGraphFile` associated to the provided `ModuleSpecifier`.
///
/// If the `specifier` is associated with a file that has a populated redirect field,
/// it returns the file associated to the redirect, otherwise the file associated to `specifier`.
fn get_resolved_file<'a>(
graph: &'a ModuleGraph,
specifier: &ModuleSpecifier,
) -> &'a ModuleGraphFile {
// Note(kc): This code is dependent on how we are injecting a dummy ModuleGraphFile
// into the graph with a "redirect" property.
let result = graph.get(specifier.as_str()).unwrap();
if let Some(ref import) = result.redirect {
graph.get(import).unwrap()
} else {
result
}
}
/// Prints the `FileInfoDepTree` tree to stdout.
fn print_file_dep_info(
info: &FileInfoDepTree,
prefix: &str,
is_last: bool,
formatter: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
print_dep(prefix, is_last, info, formatter)?;
let prefix = &get_new_prefix(prefix, is_last);
let child_count = info.deps.len();
for (idx, dep) in info.deps.iter().enumerate() {
print_file_dep_info(dep, prefix, idx == child_count - 1, formatter)?;
}
Ok(())
}
/// Prints a single `FileInfoDepTree` to stdout.
fn print_dep(
prefix: &str,
is_last: bool,
info: &FileInfoDepTree,
formatter: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
let has_children = !info.deps.is_empty();
formatter.write_fmt(format_args!(
"{} {}{}\n",
colors::gray(&format!(
"{}{}─{}",
prefix,
get_sibling_connector(is_last),
get_child_connector(has_children),
))
.to_string(),
info.name,
get_formatted_totals(info)
))
}
/// Gets the formatted totals for the provided `FileInfoDepTree`.
///
/// If the total size is reported as 0 then an empty string is returned.
fn get_formatted_totals(info: &FileInfoDepTree) -> String {
if let Some(_total_size) = info.total_size {
colors::gray(&format!(" ({})", human_size(info.size as f64),)).to_string()
} else {
// This dependency has already been displayed somewhere else in the tree.
colors::gray(" *").to_string()
}
}
/// Gets the sibling portion of the tree branch.
fn get_sibling_connector(is_last: bool) -> char {
if is_last {
'└'
} else {
'├'
}
}
/// Gets the child connector for the branch.
fn get_child_connector(has_children: bool) -> char {
if has_children {
'┬'
} else {
'─'
}
}
/// Creates a new prefix for a dependency tree item.
fn get_new_prefix(prefix: &str, is_last: bool) -> String {
let mut prefix = prefix.to_string();
if is_last {
prefix.push(' ');
} else {
prefix.push('│');
}
prefix.push(' ');
prefix
}
pub fn human_size(bytse: f64) -> String {
let negative = if bytse.is_sign_positive() { "" } else { "-" };
let bytse = bytse.abs();
let units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
if bytse < 1_f64 {
return format!("{}{}{}", negative, bytse, "B");
}
let delimiter = 1024_f64;
let exponent = std::cmp::min(
(bytse.ln() / delimiter.ln()).floor() as i32,
(units.len() - 1) as i32,
);
let pretty_bytes = format!("{:.2}", bytse / delimiter.powi(exponent))
.parse::<f64>()
.unwrap()
* 1_f64;
let unit = units[exponent as usize];
format!("{}{}{}", negative, pretty_bytes, unit)
}
#[cfg(test)]
mod test {
use super::*;
use crate::ast::Location;
use crate::media_type::MediaType;
use crate::module_graph::ImportDescriptor;
use deno_core::url::Url;
#[test]
fn human_size_test() {
assert_eq!(human_size(16_f64), "16B");
assert_eq!(human_size((16 * 1024) as f64), "16KB");
assert_eq!(human_size((16 * 1024 * 1024) as f64), "16MB");
assert_eq!(human_size(16_f64 * 1024_f64.powf(3.0)), "16GB");
assert_eq!(human_size(16_f64 * 1024_f64.powf(4.0)), "16TB");
assert_eq!(human_size(16_f64 * 1024_f64.powf(5.0)), "16PB");
assert_eq!(human_size(16_f64 * 1024_f64.powf(6.0)), "16EB");
assert_eq!(human_size(16_f64 * 1024_f64.powf(7.0)), "16ZB");
assert_eq!(human_size(16_f64 * 1024_f64.powf(8.0)), "16YB");
}
#[test]
fn get_new_prefix_adds_spaces_if_is_last() {
let prefix = get_new_prefix("", true);
assert_eq!(prefix, " ".to_string());
}
#[test]
fn get_new_prefix_adds_a_vertical_bar_if_not_is_last() {
let prefix = get_new_prefix("", false);
assert_eq!(prefix, "│ ".to_string());
}
fn create_mock_file(
name: &str,
imports: Vec<ModuleSpecifier>,
redirect: Option<ModuleSpecifier>,
) -> (ModuleGraphFile, ModuleSpecifier) {
let spec =
ModuleSpecifier::from(Url::parse(&format!("http://{}", name)).unwrap());
let file = ModuleGraphFile {
filename: "name".to_string(),
imports: imports
.iter()
.map(|import| ImportDescriptor {
specifier: import.to_string(),
resolved_specifier: import.clone(),
resolved_type_directive: None,
type_directive: None,
location: Location {
col: 0,
filename: "".to_string(),
line: 0,
},
})
.collect(),
lib_directives: vec![],
media_type: MediaType::TypeScript,
redirect: redirect.map(|x| x.to_string()),
referenced_files: vec![],
source_code: "".to_string(),
specifier: spec.to_string(),
type_headers: vec![],
types_directives: vec![],
version_hash: "".to_string(),
url: "".to_string(),
};
(file, spec)
}
#[test]
fn get_resolved_file_test() {
let (test_file_redirect, redirect) =
create_mock_file("test_redirect", vec![], None);
let (test_file, original) =
create_mock_file("test", vec![], Some(redirect.clone()));
let mut graph = ModuleGraph::new();
graph.insert(original.to_string(), test_file);
graph.insert(redirect.to_string(), test_file_redirect);
let file = get_resolved_file(&graph, &original);
assert_eq!(file.specifier, redirect.to_string());
}
#[test]
fn dependency_count_no_redirects() {
let (a, aspec) = create_mock_file("a", vec![], None);
let (b, bspec) = create_mock_file("b", vec![aspec.clone()], None);
let (c, cspec) = create_mock_file("c", vec![bspec.clone()], None);
let mut graph = ModuleGraph::new();
graph.insert(aspec.to_string(), a);
graph.insert(bspec.to_string(), b);
graph.insert(cspec.to_string(), c);
let count = get_unique_dep_count(&graph);
assert_eq!(graph.len(), count);
}
#[test]
fn dependency_count_with_redirects() {
let (a, aspec) = create_mock_file("a", vec![], None);
let (b, bspec) = create_mock_file("b", vec![], Some(aspec.clone()));
let (c, cspec) = create_mock_file("c", vec![bspec.clone()], None);
let mut graph = ModuleGraph::new();
graph.insert(aspec.to_string(), a);
graph.insert(bspec.to_string(), b);
graph.insert(cspec.to_string(), c);
let count = get_unique_dep_count(&graph);
assert_eq!(graph.len() - 1, count);
}
}
|