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
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
use errors::DenoError;
use errors::DenoResult;
use fs;
use net;
use sha1;
use std;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::result::Result;
use url;
use url::Url;
#[cfg(test)]
use tempfile::TempDir;
pub struct DenoDir {
// Example: /Users/rld/.deno/
pub root: PathBuf,
// In the Go code this was called SrcDir.
// This is where we cache http resources. Example:
// /Users/rld/.deno/deps/github.com/ry/blah.js
pub gen: PathBuf,
// In the Go code this was called CacheDir.
// This is where we cache compilation outputs. Example:
// /Users/rld/.deno/gen/f39a473452321cacd7c346a870efb0e3e1264b43.js
pub deps: PathBuf,
// If remote resources should be reloaded.
reload: 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(
reload: bool,
custom_root: Option<&Path>,
) -> std::io::Result<DenoDir> {
// Only setup once.
let home_dir = std::env::home_dir().expect("Could not get home directory.");
let default = home_dir.join(".deno");
let root: PathBuf = match custom_root {
None => default,
Some(path) => path.to_path_buf(),
};
let gen = root.as_path().join("gen");
let deps = root.as_path().join("deps");
let deno_dir = DenoDir {
root,
gen,
deps,
reload,
};
fs::mkdir(deno_dir.gen.as_ref())?;
fs::mkdir(deno_dir.deps.as_ref())?;
debug!("root {}", deno_dir.root.display());
debug!("gen {}", deno_dir.gen.display());
debug!("deps {}", deno_dir.deps.display());
Ok(deno_dir)
}
// https://github.com/denoland/deno/blob/golang/deno_dir.go#L32-L35
pub fn cache_path(
self: &DenoDir,
filename: &str,
source_code: &str,
) -> PathBuf {
let cache_key = source_code_hash(filename, source_code);
self.gen.join(cache_key + ".js")
}
fn load_cache(
self: &DenoDir,
filename: &str,
source_code: &str,
) -> std::io::Result<String> {
let path = self.cache_path(filename, source_code);
debug!("load_cache {}", path.display());
fs::read_file_sync_string(&path)
}
pub fn code_cache(
self: &DenoDir,
filename: &str,
source_code: &str,
output_code: &str,
) -> std::io::Result<()> {
let cache_path = self.cache_path(filename, source_code);
// TODO(ry) This is a race condition w.r.t to exists() -- probably should
// create the file in exclusive mode. A worry is what might happen is there
// are two processes and one reads the cache file while the other is in the
// midst of writing it.
if cache_path.exists() {
Ok(())
} else {
let mut file = File::create(cache_path)?;
file.write_all(output_code.as_bytes())?;
Ok(())
}
}
// Prototype https://github.com/denoland/deno/blob/golang/deno_dir.go#L37-L73
fn fetch_remote_source(
self: &DenoDir,
module_name: &str,
filename: &str,
) -> DenoResult<String> {
let p = Path::new(filename);
let src = if self.reload || !p.exists() {
println!("Downloading {}", module_name);
let source = net::fetch_sync_string(module_name)?;
match p.parent() {
Some(ref parent) => std::fs::create_dir_all(parent),
None => Ok(()),
}?;
fs::write_file_sync(&p, source.as_bytes())?;
source
} else {
let source = fs::read_file_sync_string(&p)?;
source
};
Ok(src)
}
// Prototype: https://github.com/denoland/deno/blob/golang/os.go#L122-L138
fn get_source_code(
self: &DenoDir,
module_name: &str,
filename: &str,
) -> DenoResult<String> {
if is_remote(module_name) {
self.fetch_remote_source(module_name, filename)
} else if module_name.starts_with(ASSET_PREFIX) {
panic!("Asset resolution should be done in JS, not Rust.");
} else {
assert!(
module_name == filename,
"if a module isn't remote, it should have the same filename"
);
let src = fs::read_file_sync_string(Path::new(filename))?;
Ok(src)
}
}
pub fn code_fetch(
self: &DenoDir,
module_specifier: &str,
containing_file: &str,
) -> Result<CodeFetchOutput, DenoError> {
let (module_name, filename) =
self.resolve_module(module_specifier, containing_file)?;
debug!(
"code_fetch. module_name = {} module_specifier = {} containing_file = {} filename = {}",
module_name, module_specifier, containing_file, filename
);
let out = self
.get_source_code(module_name.as_str(), filename.as_str())
.and_then(|source_code| {
Ok(CodeFetchOutput {
module_name,
filename,
source_code,
maybe_output_code: None,
})
})?;
let result =
self.load_cache(out.filename.as_str(), out.source_code.as_str());
match result {
Err(err) => {
if err.kind() == std::io::ErrorKind::NotFound {
Ok(out)
} else {
Err(err.into())
}
}
Ok(output_code) => Ok(CodeFetchOutput {
module_name: out.module_name,
filename: out.filename,
source_code: out.source_code,
maybe_output_code: Some(output_code),
}),
}
}
// Prototype: https://github.com/denoland/deno/blob/golang/os.go#L56-L68
#[allow(dead_code)]
fn src_file_to_url<P: AsRef<Path>>(self: &DenoDir, filename: P) -> String {
let filename = filename.as_ref().to_path_buf();
if filename.starts_with(&self.deps) {
let rest = filename.strip_prefix(&self.deps).unwrap();
"http://".to_string() + rest.to_str().unwrap()
} else {
String::from(filename.to_str().unwrap())
}
}
// Prototype: https://github.com/denoland/deno/blob/golang/os.go#L70-L98
// Returns (module name, local filename)
fn resolve_module(
self: &DenoDir,
module_specifier: &str,
containing_file: &str,
) -> Result<(String, String), url::ParseError> {
let module_name;
let filename;
debug!(
"resolve_module before module_specifier {} containing_file {}",
module_specifier, containing_file
);
//let module_specifier = src_file_to_url(module_specifier);
//let containing_file = src_file_to_url(containing_file);
//let base_url = Url::parse(&containing_file)?;
let j: Url =
if containing_file == "." || Path::new(module_specifier).is_absolute() {
if module_specifier.starts_with("http://") {
Url::parse(module_specifier)?
} else {
Url::from_file_path(module_specifier).unwrap()
}
} else if containing_file.ends_with("/") {
let r = Url::from_directory_path(&containing_file);
// TODO(ry) Properly handle error.
if r.is_err() {
error!("Url::from_directory_path error {}", containing_file);
}
let base = r.unwrap();
base.join(module_specifier)?
} else {
let r = Url::from_file_path(&containing_file);
// TODO(ry) Properly handle error.
if r.is_err() {
error!("Url::from_file_path error {}", containing_file);
}
let base = r.unwrap();
base.join(module_specifier)?
};
match j.scheme() {
"file" => {
let mut p = j
.to_file_path()
.unwrap()
.into_os_string()
.into_string()
.unwrap();
if cfg!(target_os = "windows") {
// On windows, replace backward slashes to forward slashes.
// TODO(piscisaureus): This may not me be right, I just did it to make
// the tests pass.
p = p.replace("\\", "/");
}
module_name = p.to_string();
filename = p.to_string();
}
_ => {
module_name = module_specifier.to_string();
filename = get_cache_filename(self.deps.as_path(), j)
.to_str()
.unwrap()
.to_string();
}
}
debug!("module_name: {}, filename: {}", module_name, filename);
Ok((module_name, filename))
}
}
fn get_cache_filename(basedir: &Path, url: Url) -> PathBuf {
let mut out = basedir.to_path_buf();
out.push(url.host_str().unwrap());
for path_seg in url.path_segments().unwrap() {
out.push(path_seg);
}
out
}
#[test]
fn test_get_cache_filename() {
let url = Url::parse("http://example.com:1234/path/to/file.ts").unwrap();
let basedir = Path::new("/cache/dir/");
let cache_file = get_cache_filename(&basedir, url);
assert_eq!(
cache_file,
Path::new("/cache/dir/example.com/path/to/file.ts")
);
}
#[derive(Debug)]
pub struct CodeFetchOutput {
pub module_name: String,
pub filename: String,
pub source_code: String,
pub maybe_output_code: Option<String>,
}
#[cfg(test)]
pub fn test_setup() -> (TempDir, DenoDir) {
let temp_dir = TempDir::new().expect("tempdir fail");
let deno_dir =
DenoDir::new(false, Some(temp_dir.path())).expect("setup fail");
(temp_dir, deno_dir)
}
#[test]
fn test_cache_path() {
let (temp_dir, deno_dir) = test_setup();
assert_eq!(
temp_dir
.path()
.join("gen/a3e29aece8d35a19bf9da2bb1c086af71fb36ed5.js"),
deno_dir.cache_path("hello.ts", "1+2")
);
}
#[test]
fn test_code_cache() {
let (_temp_dir, deno_dir) = test_setup();
let filename = "hello.js";
let source_code = "1+2";
let output_code = "1+2 // output code";
let cache_path = deno_dir.cache_path(filename, source_code);
assert!(
cache_path.ends_with("gen/e8e3ee6bee4aef2ec63f6ec3db7fc5fdfae910ae.js")
);
let r = deno_dir.code_cache(filename, source_code, output_code);
r.expect("code_cache error");
assert!(cache_path.exists());
assert_eq!(output_code, fs::read_file_sync_string(&cache_path).unwrap());
}
// https://github.com/denoland/deno/blob/golang/deno_dir.go#L25-L30
fn source_code_hash(filename: &str, source_code: &str) -> String {
let mut m = sha1::Sha1::new();
m.update(filename.as_bytes());
m.update(source_code.as_bytes());
m.digest().to_string()
}
#[test]
fn test_source_code_hash() {
assert_eq!(
"a3e29aece8d35a19bf9da2bb1c086af71fb36ed5",
source_code_hash("hello.ts", "1+2")
);
// Different source_code should result in different hash.
assert_eq!(
"914352911fc9c85170908ede3df1128d690dda41",
source_code_hash("hello.ts", "1")
);
// Different filename should result in different hash.
assert_eq!(
"2e396bc66101ecc642db27507048376d972b1b70",
source_code_hash("hi.ts", "1+2")
);
}
// The `add_root` macro prepends "C:" to a string if on windows; on posix
// systems it returns the input string untouched. This is necessary because
// `Url::from_file_path()` fails if the input path isn't an absolute path.
#[cfg(test)]
macro_rules! add_root {
($path:expr) => {
if cfg!(target_os = "windows") {
concat!("C:", $path)
} else {
$path
}
};
}
#[test]
fn test_code_fetch() {
let (_temp_dir, deno_dir) = test_setup();
let cwd = std::env::current_dir().unwrap();
let cwd_string = String::from(cwd.to_str().unwrap()) + "/";
// Test failure case.
let module_specifier = "hello.ts";
let containing_file = add_root!("/baddir/badfile.ts");
let r = deno_dir.code_fetch(module_specifier, containing_file);
assert!(r.is_err());
// Assuming cwd is the deno repo root.
let module_specifier = "./js/main.ts";
let containing_file = cwd_string.as_str();
let r = deno_dir.code_fetch(module_specifier, containing_file);
assert!(r.is_ok());
//let code_fetch_output = r.unwrap();
//println!("code_fetch_output {:?}", code_fetch_output);
}
#[test]
fn test_src_file_to_url() {
let (_temp_dir, deno_dir) = test_setup();
assert_eq!("hello", deno_dir.src_file_to_url("hello"));
assert_eq!("/hello", deno_dir.src_file_to_url("/hello"));
let x = String::from(deno_dir.deps.join("hello/world.txt").to_str().unwrap());
assert_eq!("http://hello/world.txt", deno_dir.src_file_to_url(x));
}
// https://github.com/denoland/deno/blob/golang/os_test.go#L16-L87
#[test]
fn test_resolve_module() {
let (_temp_dir, deno_dir) = test_setup();
let test_cases = [
(
"./subdir/print_hello.ts",
add_root!(
"/Users/rld/go/src/github.com/denoland/deno/testdata/006_url_imports.ts"
),
add_root!(
"/Users/rld/go/src/github.com/denoland/deno/testdata/subdir/print_hello.ts"
),
add_root!(
"/Users/rld/go/src/github.com/denoland/deno/testdata/subdir/print_hello.ts"
),
),
(
"testdata/001_hello.js",
add_root!("/Users/rld/go/src/github.com/denoland/deno/"),
add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/001_hello.js"),
add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/001_hello.js"),
),
(
add_root!("/Users/rld/src/deno/hello.js"),
".",
add_root!("/Users/rld/src/deno/hello.js"),
add_root!("/Users/rld/src/deno/hello.js"),
),
(
add_root!("/this/module/got/imported.js"),
add_root!("/that/module/did/it.js"),
add_root!("/this/module/got/imported.js"),
add_root!("/this/module/got/imported.js"),
),
/*
(
"http://localhost:4545/testdata/subdir/print_hello.ts",
add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/006_url_imports.ts"),
"http://localhost:4545/testdata/subdir/print_hello.ts",
path.Join(SrcDir, "localhost:4545/testdata/subdir/print_hello.ts"),
),
(
path.Join(SrcDir, "unpkg.com/liltest@0.0.5/index.ts"),
".",
"http://unpkg.com/liltest@0.0.5/index.ts",
path.Join(SrcDir, "unpkg.com/liltest@0.0.5/index.ts"),
),
(
"./util",
path.Join(SrcDir, "unpkg.com/liltest@0.0.5/index.ts"),
"http://unpkg.com/liltest@0.0.5/util",
path.Join(SrcDir, "unpkg.com/liltest@0.0.5/util"),
),
*/
];
for &test in test_cases.iter() {
let module_specifier = String::from(test.0);
let containing_file = String::from(test.1);
let (module_name, filename) = deno_dir
.resolve_module(&module_specifier, &containing_file)
.unwrap();
assert_eq!(module_name, test.2);
assert_eq!(filename, test.3);
}
}
const ASSET_PREFIX: &str = "/$asset$/";
fn is_remote(module_name: &str) -> bool {
module_name.starts_with("http")
}
|