-
-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
176e317
commit a033c3b
Showing
10 changed files
with
221 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
MIT License | ||
|
||
Copyright 2018-2022 the Deno authors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
//! Loader stubs to be used to resolve JavaScript dependencies | ||
use std::{fs::read_to_string, path::Path, pin::Pin}; | ||
|
||
use deno_ast::{MediaType, ParseParams, SourceTextInfo}; | ||
use deno_core::{ | ||
anyhow::bail, futures::FutureExt, url::Url, ModuleLoader, ModuleSource, ModuleSourceFuture, | ||
ModuleSpecifier, ModuleType, | ||
}; | ||
use import_map::ImportMapWithDiagnostics; | ||
use libafl::Error; | ||
|
||
/// Loader which loads dependencies in vendored deno environments. | ||
#[derive(Debug)] | ||
pub struct VendoredLoader { | ||
import_map: ImportMapWithDiagnostics, | ||
} | ||
|
||
impl VendoredLoader { | ||
/// Create a new vendored loader with a path to the vendor/import_map.json. | ||
pub fn new<P: AsRef<Path>>(map_path: P) -> Result<Self, Error> { | ||
let json = read_to_string(map_path.as_ref())?; | ||
let url = match Url::from_file_path(map_path.as_ref()) { | ||
Ok(u) => u, | ||
Err(_) => { | ||
return Err(Error::illegal_argument(format!( | ||
"Path was not found or was not absolute: {}", | ||
map_path.as_ref().to_str().unwrap() | ||
))) | ||
} | ||
}; | ||
if let Ok(import_map) = import_map::parse_from_json(&url, &json) { | ||
Ok(Self { import_map }) | ||
} else { | ||
Err(Error::illegal_state( | ||
"Couldn't parse the provided import map", | ||
)) | ||
} | ||
} | ||
|
||
fn read_file_specifier(module_specifier: ModuleSpecifier) -> Pin<Box<ModuleSourceFuture>> { | ||
if let Ok(path) = module_specifier.to_file_path() { | ||
async move { | ||
// Section surrounded by SNIP below is taken from: https://github.com/denoland/deno/blob/94d369ebc65a55bd9fbf378a765c8ed88a4efe2c/core/examples/ts_module_loader.rs#L51-L88 | ||
// License text available at: ../LICENSE-DENO | ||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. | ||
// ---- SNIP ---- | ||
let media_type = MediaType::from(&path); | ||
let (module_type, should_transpile) = match MediaType::from(&path) { | ||
MediaType::JavaScript | MediaType::Mjs | MediaType::Cjs => { | ||
(ModuleType::JavaScript, false) | ||
} | ||
MediaType::Jsx => (ModuleType::JavaScript, true), | ||
MediaType::TypeScript | ||
| MediaType::Mts | ||
| MediaType::Cts | ||
| MediaType::Dts | ||
| MediaType::Dmts | ||
| MediaType::Dcts | ||
| MediaType::Tsx => (ModuleType::JavaScript, true), | ||
MediaType::Json => (ModuleType::Json, false), | ||
_ => bail!("Unknown extension {:?}", path.extension()), | ||
}; | ||
|
||
let code = read_to_string(&path)?; | ||
let code = if should_transpile { | ||
let parsed = deno_ast::parse_module(ParseParams { | ||
specifier: module_specifier.to_string(), | ||
text_info: SourceTextInfo::from_string(code), | ||
media_type, | ||
capture_tokens: false, | ||
scope_analysis: false, | ||
maybe_syntax: None, | ||
})?; | ||
parsed.transpile(&Default::default())?.text | ||
} else { | ||
code | ||
}; | ||
let module = ModuleSource { | ||
code: code.into_bytes().into_boxed_slice(), | ||
module_type, | ||
module_url_specified: module_specifier.to_string(), | ||
module_url_found: module_specifier.to_string(), | ||
}; | ||
Ok(module) | ||
// ---- SNIP ---- | ||
} | ||
.boxed_local() | ||
} else { | ||
unimplemented!("File URL couldn't be parsed") | ||
} | ||
} | ||
} | ||
|
||
impl ModuleLoader for VendoredLoader { | ||
fn resolve( | ||
&self, | ||
specifier: &str, | ||
referrer: &str, | ||
_is_main: bool, | ||
) -> Result<ModuleSpecifier, deno_core::anyhow::Error> { | ||
let referrer = deno_core::resolve_url_or_path(referrer)?; | ||
Ok(self.import_map.import_map.resolve(specifier, &referrer)?) | ||
} | ||
|
||
fn load( | ||
&self, | ||
module_specifier: &ModuleSpecifier, | ||
_maybe_referrer: Option<ModuleSpecifier>, | ||
_is_dyn_import: bool, | ||
) -> Pin<Box<ModuleSourceFuture>> { | ||
let module_specifier = module_specifier.clone(); | ||
if module_specifier.scheme() == "file" { | ||
Self::read_file_specifier(module_specifier) | ||
} else { | ||
unimplemented!("Not attempting to resolve non-file module specifiers") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.