diff options
Diffstat (limited to 'cli/ops')
-rw-r--r-- | cli/ops/compiler.rs | 46 | ||||
-rw-r--r-- | cli/ops/mod.rs | 6 |
2 files changed, 31 insertions, 21 deletions
diff --git a/cli/ops/compiler.rs b/cli/ops/compiler.rs index 40d25aa74..975e01ee1 100644 --- a/cli/ops/compiler.rs +++ b/cli/ops/compiler.rs @@ -33,39 +33,49 @@ pub fn op_cache( } #[derive(Deserialize)] -struct FetchSourceFileArgs { - specifier: String, +struct FetchSourceFilesArgs { + specifiers: Vec<String>, referrer: String, } -pub fn op_fetch_source_file( +pub fn op_fetch_source_files( state: &ThreadSafeState, args: Value, _zero_copy: Option<PinnedBuf>, ) -> Result<JsonOp, ErrBox> { - let args: FetchSourceFileArgs = serde_json::from_value(args)?; + let args: FetchSourceFilesArgs = serde_json::from_value(args)?; // TODO(ry) Maybe a security hole. Only the compiler worker should have access // to this. Need a test to demonstrate the hole. let is_dyn_import = false; - let resolved_specifier = - state.resolve(&args.specifier, &args.referrer, false, is_dyn_import)?; - - let fut = state - .file_fetcher - .fetch_source_file_async(&resolved_specifier); + let mut futures = vec![]; + for specifier in &args.specifiers { + let resolved_specifier = + state.resolve(specifier, &args.referrer, false, is_dyn_import)?; + let fut = state + .file_fetcher + .fetch_source_file_async(&resolved_specifier); + futures.push(fut); + } // WARNING: Here we use tokio_util::block_on() which starts a new Tokio - // runtime for executing the future. This is so we don't inadvernently run + // runtime for executing the future. This is so we don't inadvertently run // out of threads in the main runtime. - let out = tokio_util::block_on(fut)?; - Ok(JsonOp::Sync(json!({ - "moduleName": out.url.to_string(), - "filename": out.filename.to_str().unwrap(), - "mediaType": out.media_type as i32, - "sourceCode": String::from_utf8(out.source_code).unwrap(), - }))) + let files = tokio_util::block_on(futures::future::join_all(futures))?; + let res: Vec<serde_json::value::Value> = files + .into_iter() + .map(|file| { + json!({ + "moduleName": file.url.to_string(), + "filename": file.filename.to_str().unwrap(), + "mediaType": file.media_type as i32, + "sourceCode": String::from_utf8(file.source_code).unwrap(), + }) + }) + .collect(); + + Ok(JsonOp::Sync(json!(res))) } #[derive(Deserialize)] diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index 1f07acc65..7a2e9c9f4 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -37,7 +37,7 @@ pub const OP_START: OpId = 10; pub const OP_APPLY_SOURCE_MAP: OpId = 11; pub const OP_FORMAT_ERROR: OpId = 12; pub const OP_CACHE: OpId = 13; -pub const OP_FETCH_SOURCE_FILE: OpId = 14; +pub const OP_FETCH_SOURCE_FILES: OpId = 14; pub const OP_OPEN: OpId = 15; pub const OP_CLOSE: OpId = 16; pub const OP_SEEK: OpId = 17; @@ -133,8 +133,8 @@ pub fn dispatch( OP_CACHE => { dispatch_json::dispatch(compiler::op_cache, state, control, zero_copy) } - OP_FETCH_SOURCE_FILE => dispatch_json::dispatch( - compiler::op_fetch_source_file, + OP_FETCH_SOURCE_FILES => dispatch_json::dispatch( + compiler::op_fetch_source_files, state, control, zero_copy, |