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 | |
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.
-rw-r--r-- | cli/compilers/json.rs | 50 | ||||
-rw-r--r-- | cli/compilers/mod.rs | 2 | ||||
-rw-r--r-- | cli/global_state.rs | 8 | ||||
-rw-r--r-- | cli/js/compiler/imports.ts | 2 | ||||
-rw-r--r-- | cli/js/compiler/sourcefile.ts | 5 | ||||
-rw-r--r-- | cli/js/compiler/util.ts | 12 | ||||
-rw-r--r-- | cli/ops/compiler.rs | 8 | ||||
-rw-r--r-- | cli/tests/020_json_modules.ts.out | 10 | ||||
-rw-r--r-- | cli/tests/050_more_jsons.ts | 7 | ||||
-rw-r--r-- | cli/tests/050_more_jsons.ts.out | 9 | ||||
-rw-r--r-- | cli/tests/integration_tests.rs | 39 |
11 files changed, 15 insertions, 137 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(), - }) - } -} diff --git a/cli/compilers/mod.rs b/cli/compilers/mod.rs index e30c89173..f773e2de6 100644 --- a/cli/compilers/mod.rs +++ b/cli/compilers/mod.rs @@ -5,12 +5,10 @@ use futures::Future; mod compiler_worker; mod js; -mod json; mod ts; mod wasm; pub use js::JsCompiler; -pub use json::JsonCompiler; pub use ts::runtime_compile; pub use ts::runtime_transpile; pub use ts::TargetLib; diff --git a/cli/global_state.rs b/cli/global_state.rs index c9383bd88..cee105d4d 100644 --- a/cli/global_state.rs +++ b/cli/global_state.rs @@ -1,7 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. use crate::compilers::CompiledModule; use crate::compilers::JsCompiler; -use crate::compilers::JsonCompiler; use crate::compilers::TargetLib; use crate::compilers::TsCompiler; use crate::compilers::WasmCompiler; @@ -36,7 +35,6 @@ pub struct GlobalStateInner { pub dir: deno_dir::DenoDir, pub file_fetcher: SourceFileFetcher, pub js_compiler: JsCompiler, - pub json_compiler: JsonCompiler, pub ts_compiler: TsCompiler, pub wasm_compiler: WasmCompiler, pub lockfile: Option<Mutex<Lockfile>>, @@ -88,7 +86,6 @@ impl GlobalState { file_fetcher, ts_compiler, js_compiler: JsCompiler {}, - json_compiler: JsonCompiler {}, wasm_compiler: WasmCompiler::default(), lockfile, compiler_starts: AtomicUsize::new(0), @@ -118,8 +115,9 @@ impl GlobalState { let compile_lock = self.compile_lock.lock().await; let compiled_module = match out.media_type { - msg::MediaType::Unknown => state1.js_compiler.compile(out).await, - msg::MediaType::Json => state1.json_compiler.compile(&out).await, + msg::MediaType::Json | msg::MediaType::Unknown => { + state1.js_compiler.compile(out).await + } msg::MediaType::Wasm => { state1.wasm_compiler.compile(state1.clone(), &out).await } diff --git a/cli/js/compiler/imports.ts b/cli/js/compiler/imports.ts index 6b48ec945..4261a4123 100644 --- a/cli/js/compiler/imports.ts +++ b/cli/js/compiler/imports.ts @@ -83,8 +83,6 @@ function getMediaType(filename: string): MediaType { return MediaType.JavaScript; case "jsx": return MediaType.JSX; - case "json": - return MediaType.Json; case "ts": return MediaType.TypeScript; case "tsx": diff --git a/cli/js/compiler/sourcefile.ts b/cli/js/compiler/sourcefile.ts index a55de080b..3d547551f 100644 --- a/cli/js/compiler/sourcefile.ts +++ b/cli/js/compiler/sourcefile.ts @@ -34,11 +34,6 @@ function getExtension(fileName: string, mediaType: MediaType): ts.Extension { return fileName.endsWith(".d.ts") ? ts.Extension.Dts : ts.Extension.Ts; case MediaType.TSX: return ts.Extension.Tsx; - case MediaType.Json: - // we internally compile JSON, so what gets provided to the TypeScript - // compiler is an ES module, but in order to get TypeScript to handle it - // properly we have to pretend it is TS. - return ts.Extension.Ts; case MediaType.Wasm: // Custom marker for Wasm type. return ts.Extension.Js; diff --git a/cli/js/compiler/util.ts b/cli/js/compiler/util.ts index 170dff30f..d461fcbbb 100644 --- a/cli/js/compiler/util.ts +++ b/cli/js/compiler/util.ts @@ -48,13 +48,6 @@ function cache( const sf = SourceFile.get(moduleId); if (sf) { - // NOTE: If it's a `.json` file we don't want to write it to disk. - // JSON files are loaded and used by TS compiler to check types, but we don't want - // to emit them to disk because output file is the same as input file. - if (sf.mediaType === MediaType.Json) { - return; - } - // NOTE: JavaScript files are only cached to disk if `checkJs` // option in on if (sf.mediaType === MediaType.JavaScript && !checkJs) { @@ -65,10 +58,7 @@ function cache( if (emittedFileName.endsWith(".map")) { // Source Map compilerOps.cache(".map", moduleId, contents); - } else if ( - emittedFileName.endsWith(".js") || - emittedFileName.endsWith(".json") - ) { + } else if (emittedFileName.endsWith(".js")) { // Compiled JavaScript compilerOps.cache(".js", moduleId, contents); } else { diff --git a/cli/ops/compiler.rs b/cli/ops/compiler.rs index 32e6655f9..1029070a8 100644 --- a/cli/ops/compiler.rs +++ b/cli/ops/compiler.rs @@ -149,14 +149,6 @@ fn op_fetch_source_files( .map_err(|e| OpError::other(e.to_string()))? .code } - msg::MediaType::Json => { - global_state - .json_compiler - .compile(&file) - .await - .map_err(|e| OpError::other(e.to_string()))? - .code - } _ => String::from_utf8(file.source_code) .map_err(|_| OpError::invalid_utf8())?, }; diff --git a/cli/tests/020_json_modules.ts.out b/cli/tests/020_json_modules.ts.out index 5d1623e6b..02106dafc 100644 --- a/cli/tests/020_json_modules.ts.out +++ b/cli/tests/020_json_modules.ts.out @@ -1 +1,9 @@ -{"foo":{"bar":true,"baz":["qat",1]}} +[WILDCARD] +error: Uncaught TypeError: Cannot resolve extension for "[WILDCARD]config.json" with mediaType "Json". + at getExtension ($deno$/compiler/sourcefile.ts:[WILDCARD]) + at new SourceFile ($deno$/compiler/sourcefile.ts:[WILDCARD]) + at processImports ($deno$/compiler/imports.ts:[WILDCARD]) + at async Object.processImports ($deno$/compiler/imports.ts:[WILDCARD]) + at async compile ([WILDCARD]compiler.ts:[WILDCARD]) + at async tsCompilerOnMessage ([WILDCARD]compiler.ts:[WILDCARD]) + at async workerMessageRecvCallback ($deno$/runtime_worker.ts:[WILDCARD]) diff --git a/cli/tests/050_more_jsons.ts b/cli/tests/050_more_jsons.ts deleted file mode 100644 index 90deabcd1..000000000 --- a/cli/tests/050_more_jsons.ts +++ /dev/null @@ -1,7 +0,0 @@ -import j1, { $var } from "./subdir/json_1.json"; -import j2 from "./subdir/json_2.json"; -console.log($var); -console.log($var.a); -console.log(j1); -console.log(j1["with space"]); -console.log(j2); diff --git a/cli/tests/050_more_jsons.ts.out b/cli/tests/050_more_jsons.ts.out deleted file mode 100644 index c3d1e5cbf..000000000 --- a/cli/tests/050_more_jsons.ts.out +++ /dev/null @@ -1,9 +0,0 @@ -{ a: 123, b: [ 1, 2, 3 ], c: null } -123 -{ - $var: { a: 123, b: [ 1, 2, 3 ], c: null }, - with space: "invalid variable name", - function: "reserved word" -} -invalid variable name -just a string diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 953f92c66..fbaab7674 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -396,38 +396,6 @@ fn bundle_single_module() { } #[test] -fn bundle_json() { - let json_modules = util::root_path().join("cli/tests/020_json_modules.ts"); - assert!(json_modules.is_file()); - let t = TempDir::new().expect("tempdir fail"); - let bundle = t.path().join("020_json_modules.bundle.js"); - let mut deno = util::deno_cmd() - .current_dir(util::root_path()) - .arg("bundle") - .arg(json_modules) - .arg(&bundle) - .spawn() - .expect("failed to spawn script"); - let status = deno.wait().expect("failed to wait for the child process"); - assert!(status.success()); - assert!(bundle.is_file()); - - let output = util::deno_cmd() - .current_dir(util::root_path()) - .arg("run") - .arg("--reload") - .arg(&bundle) - .output() - .expect("failed to spawn script"); - // check the output of the the bundle program. - assert!(std::str::from_utf8(&output.stdout) - .unwrap() - .trim() - .ends_with("{\"foo\":{\"bar\":true,\"baz\":[\"qat\",1]}}")); - assert_eq!(output.stderr, b""); -} - -#[test] fn bundle_tla() { // First we have to generate a bundle of some module that has exports. let tla_import = util::root_path().join("cli/tests/subdir/tla.ts"); @@ -927,7 +895,9 @@ itest_ignore!(_019_media_types { itest!(_020_json_modules { args: "run --reload 020_json_modules.ts", + check_stderr: true, output: "020_json_modules.ts.out", + exit_code: 1, }); itest!(_021_mjs_modules { @@ -1127,11 +1097,6 @@ itest_ignore!(_049_info_flag_script_jsx { http_server: true, }); -itest!(_050_more_jsons { - args: "run --reload 050_more_jsons.ts", - output: "050_more_jsons.ts.out", -}); - itest!(_051_wasm_import { args: "run --reload --allow-net --allow-read 051_wasm_import.ts", output: "051_wasm_import.ts.out", |