Skip to content

Commit e0f656f

Browse files
committed
fix: don't apply fixes that can be applied by module-level requirejs-config.js - fixes #36
1 parent 82cfeea commit e0f656f

9 files changed

+277
-539
lines changed

src/lib/presets/m2/handlers/config_capture.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@ pub fn handle(original_request: &HttpRequest<AppState>) -> FutResp {
1414
let client_config_clone = original_request.state().rjs_client_config.clone();
1515
apply_to_proxy_body(&original_request, move |b| {
1616
let c2 = client_config_clone.clone();
17-
if let Ok(rjs) = RequireJsClientConfig::from_generated_string(b.to_string()) {
18-
info!("parsed rjs = {:#?}", rjs);
19-
let mut w = c2.lock().expect("unwrapped client_config_clone");
20-
w.deps = rjs.deps.clone();
21-
w.config = rjs.config.clone();
22-
w.shim = rjs.shim.clone();
23-
w.paths = rjs.paths.clone();
24-
w.map = rjs.map.clone();
25-
info!("next rjs = {:#?}", w);
26-
}
17+
match RequireJsClientConfig::update_in_place(b.to_string(), c2) {
18+
Ok(..) => {
19+
/* no op */
20+
}
21+
Err(e) => {
22+
eprintln!("Could not update `RequireJsClientConfig` in place, e = {}", e);
23+
}
24+
};
2725
b
2826
})
2927
}

src/lib/presets/m2/handlers/config_post.rs

+12-21
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,29 @@ use app_state::AppState;
44
use futures::{Future, Stream};
55
use presets::m2::preset_m2::FutResp;
66
use presets::m2::requirejs_config::RequireJsClientConfig;
7-
use serde_json;
87
use std;
98

109
///
1110
/// This handler accepts the incoming RequireJS merged
1211
/// config from the client
1312
///
14-
pub fn handle(req: &HttpRequest<AppState>) -> FutResp {
15-
let a = req.state().rjs_client_config.clone();
13+
pub fn handle(original_request: &HttpRequest<AppState>) -> FutResp {
14+
let client_config_clone = original_request.state().rjs_client_config.clone();
1615

17-
req.payload()
16+
original_request
17+
.payload()
1818
.concat2()
1919
.from_err()
2020
.and_then(move |body| {
21-
let result: Result<RequireJsClientConfig, serde_json::Error> =
22-
serde_json::from_str(std::str::from_utf8(&body).unwrap());
23-
//
24-
match result {
25-
Ok(next_config) => {
26-
let mut mutex = a.lock().unwrap();
27-
mutex.base_url = next_config.base_url;
28-
mutex.map = next_config.map;
29-
mutex.config = next_config.config;
30-
mutex.paths = next_config.paths;
31-
mutex.shim = next_config.shim;
32-
33-
Ok(HttpResponse::Ok()
21+
let c2 = client_config_clone.clone();
22+
match std::str::from_utf8(&body[..]) {
23+
Ok(body) => match RequireJsClientConfig::update_in_place(body.to_string(), c2) {
24+
Ok(()) => Ok(HttpResponse::Ok()
3425
.content_type("application/json")
35-
.body("ok"))
36-
}
26+
.body("ok")),
27+
Err(e) => Ok(super::err_response::create(e.to_string())),
28+
},
3729
Err(e) => Ok(super::err_response::create(e.to_string())),
3830
}
39-
})
40-
.responder()
31+
}).responder()
4132
}

src/lib/presets/m2/parse.rs

+36-11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ pub enum OutputError {
2222
Conversion,
2323
}
2424

25+
impl OutputError {
26+
pub fn to_string(&self) -> String {
27+
match self {
28+
OutputError::ParseJs => "OutputError::ParseJs".into(),
29+
OutputError::Serialize => "OutputError::Serialize".into(),
30+
OutputError::Conversion => "OutputError::Conversion".into(),
31+
}
32+
}
33+
}
34+
2535
impl ParsedConfig {
2636
///
2737
/// Parse a Magento 2 generated requirejs-config.js file pulling out
@@ -110,10 +120,9 @@ fn process_shim(xs: &Vec<ObjectMember>, output: &mut ParsedConfig) {
110120
.map(|s| serde_json::Value::String(s))
111121
.collect();
112122

113-
let mut map_item = output
123+
output
114124
.shim
115-
.entry(strip_literal(s))
116-
.or_insert(serde_json::Value::Array(as_serde));
125+
.insert(strip_literal(s), serde_json::Value::Array(as_serde));
117126
}
118127
Expression::Object(vs) => {
119128
let mut m = serde_json::Map::new();
@@ -154,10 +163,9 @@ fn process_shim(xs: &Vec<ObjectMember>, output: &mut ParsedConfig) {
154163
_ => {}
155164
}
156165
}
157-
let mut map_item = output
166+
output
158167
.shim
159-
.entry(strip_literal(s))
160-
.or_insert(serde_json::Value::Object(m));
168+
.insert(strip_literal(s), serde_json::Value::Object(m));
161169
}
162170
_ => { /* */ }
163171
}
@@ -178,8 +186,10 @@ fn process_config(xs: &Vec<ObjectMember>, output: &mut ParsedConfig) {
178186
key: ObjectKey::Literal(s),
179187
value,
180188
} => {
181-
let mut map_item =
182-
output.config.entry(strip_literal(s).to_string()).or_insert(HashMap::new());
189+
let mut map_item = output
190+
.config
191+
.entry(strip_literal(s).to_string())
192+
.or_insert(HashMap::new());
183193

184194
match value {
185195
Expression::Object(vs) => {
@@ -287,7 +297,10 @@ fn process_map(xs: &Vec<ObjectMember>, output: &mut ParsedConfig) {
287297
key: ObjectKey::Literal(k),
288298
value: Expression::Literal(Value::String(v)),
289299
} => {
290-
map_item.insert(strip_literal(k).to_string(), strip_literal(v));
300+
map_item.insert(
301+
strip_literal(k).to_string(),
302+
strip_literal(v),
303+
);
291304
}
292305
_ => { /* */ }
293306
}
@@ -411,6 +424,18 @@ mod tests {
411424
};
412425
require.config(config);
413426
})();
427+
(function() {
428+
var config = {
429+
shim: {
430+
"jquery/jquery-migrate": {
431+
"deps": [
432+
"jquery",
433+
'jquery/jquery.cookie'
434+
]
435+
}
436+
}
437+
}
438+
})();
414439
"#;
415440

416441
let o = ParsedConfig::from_str(input).expect("parses fixture");
@@ -431,7 +456,8 @@ mod tests {
431456
"shim": {
432457
"jquery/jquery-migrate": {
433458
"deps": [
434-
"jquery"
459+
"jquery",
460+
"jquery/jquery.cookie"
435461
]
436462
},
437463
"paypalInContextExpressCheckout": {
@@ -461,7 +487,6 @@ mod tests {
461487
let expected: serde_json::Value = serde_json::from_str(&from).expect("serde from (fixture)");
462488
let actual = serde_json::to_value(&o).expect("Output serialized");
463489

464-
// println!("{:#?}", actual);
465490
assert_eq!(actual, expected);
466491

467492
let _as_require: RequireJsClientConfig =

0 commit comments

Comments
 (0)