diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-05-02 00:32:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-02 00:32:05 +0200 |
commit | de2c042482741dc23f7d975458a1fba95863de53 (patch) | |
tree | d9cb99d069a450ce708952b980ae15857b86a5d9 /cli/compilers/json.rs | |
parent | 96fd0f4692126516239d61784caf6599aa884844 (diff) |
BREAKING: remove support for JSON imports (#5037)
This commit removes support for importing JSON files as modules.
This change is dictated by security; browsers rolled back on this
support as well.
Diffstat (limited to 'cli/compilers/json.rs')
-rw-r--r-- | cli/compilers/json.rs | 50 |
1 files changed, 0 insertions, 50 deletions
diff --git a/cli/compilers/json.rs b/cli/compilers/json.rs deleted file mode 100644 index 0b7a91af3..000000000 --- a/cli/compilers/json.rs +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use crate::compilers::CompiledModule; -use crate::file_fetcher::SourceFile; -use deno_core::ErrBox; -use regex::Regex; - -// From https://github.com/mathiasbynens/mothereff.in/blob/master/js-variables/eff.js -static JS_RESERVED_WORDS: &str = r"^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|await|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$"; - -pub struct JsonCompiler {} - -impl JsonCompiler { - pub async fn compile( - &self, - source_file: &SourceFile, - ) -> Result<CompiledModule, ErrBox> { - let maybe_json_value = serde_json::from_slice(&source_file.source_code); - if let Err(err) = maybe_json_value { - return Err(ErrBox::from(err)); - } - - let mut code = format!( - "export default {};\n", - std::str::from_utf8(&source_file.source_code).unwrap() - ); - - if let serde_json::Value::Object(m) = maybe_json_value.unwrap() { - // Best effort variable name exports - // Actual all allowed JS variable names are way tricker. - // We only handle a subset of alphanumeric names. - let js_var_regex = Regex::new(r"^[a-zA-Z_$][0-9a-zA-Z_$]*$").unwrap(); - // Also avoid collision with reserved words. - let reserved_words = Regex::new(JS_RESERVED_WORDS).unwrap(); - for (key, value) in m.iter() { - if js_var_regex.is_match(&key) && !reserved_words.is_match(&key) { - code.push_str(&format!( - "export const {} = {};\n", - key, - value.to_string() - )); - } - } - } - - Ok(CompiledModule { - code, - name: source_file.url.to_string(), - }) - } -} |