|
1 | 1 | use crate::node_obj_deserializer::JsValueDeserializer; |
2 | 2 | use crate::node_obj_serializer::NodeObjSerializer; |
| 3 | +use anyhow::anyhow; |
3 | 4 | use cubetranspilers::{run_transpilers, TransformConfig, Transpilers}; |
4 | 5 | use neon::context::{Context, FunctionContext, ModuleContext}; |
5 | 6 | use neon::prelude::{JsPromise, JsResult, JsValue, NeonResult}; |
@@ -36,43 +37,45 @@ pub fn transpile_js(mut cx: FunctionContext) -> JsResult<JsPromise> { |
36 | 37 | let content = cx.argument::<JsString>(0)?.value(&mut cx); |
37 | 38 | let transform_data_js_object = cx.argument::<JsValue>(1)?; |
38 | 39 | let deserializer = JsValueDeserializer::new(&mut cx, transform_data_js_object); |
39 | | - |
40 | | - let transform_config: TransformConfig = match TransformRequestConfig::deserialize(deserializer) |
41 | | - { |
42 | | - Ok(data) => match data.meta_data { |
43 | | - Some(meta_data) => { |
44 | | - let mut config_lock = METADATA_CACHE.write().unwrap(); |
45 | | - let cache = TransformMetaData { |
46 | | - cube_names: meta_data.cube_names, |
47 | | - cube_symbols: meta_data.cube_symbols, |
48 | | - context_symbols: meta_data.context_symbols, |
49 | | - }; |
50 | | - let cfg = TransformConfig { |
51 | | - file_name: data.file_name, |
52 | | - transpilers: data.transpilers, |
53 | | - cube_names: cache.cube_names.clone(), |
54 | | - cube_symbols: cache.cube_symbols.clone(), |
55 | | - context_symbols: cache.context_symbols.clone(), |
56 | | - }; |
57 | | - *config_lock = Some(cache); |
58 | | - cfg |
59 | | - } |
60 | | - None => { |
61 | | - let cache = METADATA_CACHE.read().unwrap().clone().unwrap_or_default(); |
62 | | - TransformConfig { |
63 | | - file_name: data.file_name, |
64 | | - transpilers: data.transpilers, |
65 | | - cube_names: cache.cube_names.clone(), |
66 | | - cube_symbols: cache.cube_symbols.clone(), |
67 | | - context_symbols: cache.context_symbols.clone(), |
68 | | - } |
69 | | - } |
70 | | - }, |
71 | | - Err(err) => return cx.throw_error(err.to_string()), |
72 | | - }; |
| 40 | + let transform_request_config = TransformRequestConfig::deserialize(deserializer); |
73 | 41 |
|
74 | 42 | let promise = cx |
75 | | - .task(move || run_transpilers(content, transform_config)) |
| 43 | + .task(move || { |
| 44 | + let transform_config: TransformConfig = match transform_request_config { |
| 45 | + Ok(data) => match data.meta_data { |
| 46 | + Some(meta_data) => { |
| 47 | + let mut config_lock = METADATA_CACHE.write().unwrap(); |
| 48 | + let cache = TransformMetaData { |
| 49 | + cube_names: meta_data.cube_names, |
| 50 | + cube_symbols: meta_data.cube_symbols, |
| 51 | + context_symbols: meta_data.context_symbols, |
| 52 | + }; |
| 53 | + let cfg = TransformConfig { |
| 54 | + file_name: data.file_name, |
| 55 | + transpilers: data.transpilers, |
| 56 | + cube_names: cache.cube_names.clone(), |
| 57 | + cube_symbols: cache.cube_symbols.clone(), |
| 58 | + context_symbols: cache.context_symbols.clone(), |
| 59 | + }; |
| 60 | + *config_lock = Some(cache); |
| 61 | + cfg |
| 62 | + } |
| 63 | + None => { |
| 64 | + let cache = METADATA_CACHE.read().unwrap().clone().unwrap_or_default(); |
| 65 | + TransformConfig { |
| 66 | + file_name: data.file_name, |
| 67 | + transpilers: data.transpilers, |
| 68 | + cube_names: cache.cube_names.clone(), |
| 69 | + cube_symbols: cache.cube_symbols.clone(), |
| 70 | + context_symbols: cache.context_symbols.clone(), |
| 71 | + } |
| 72 | + } |
| 73 | + }, |
| 74 | + Err(err) => return Err(anyhow!("Failed to deserialize input data: {}", err)), |
| 75 | + }; |
| 76 | + |
| 77 | + run_transpilers(content, transform_config) |
| 78 | + }) |
76 | 79 | .promise(move |mut cx, res| match res { |
77 | 80 | Ok(result) => { |
78 | 81 | let obj = match NodeObjSerializer::serialize(&result, &mut cx) { |
|
0 commit comments