JavaScript Transform
Source: 05_javascript_transform
An HTTP server that applies a JavaScript function to every incoming JSON payload and returns the transformed result. The JS engine is declared as a model so the function is compiled once at startup and reused across all concurrent requests.
Running
melodium run 05_javascript_transform/Compo.toml$ curl -X POST http://127.0.0.1:8080/transform \
-H "Content-Type: application/json" \
-d '{"name":"Alice","score":82}'
{"name":"Alice","score":82,"grade":"B","note":"processed by Mélodium JS engine"}How it works
The Transformer model embeds the JavaScript function directly in the source:
model Transformer() : JavaScriptEngine {
code = "
function transform(input) {
var grade = 'F';
if (input.score >= 90) grade = 'A';
…
return { name: input.name, score: input.score, grade: grade, … };
}
"
}The JS engine compiles this function once when the model is instantiated. Every request then calls process[engine=engine](code="transform(value)") which invokes the compiled function with the parsed JSON value.
The jsTransform sub-treatment
Self.data -> decode.data,text -> toJson.text,json -> unwrap.option,value
-> process.value,result -> unwrapOr.option,value
-> toString.value,into -> encode.text,data -> Self.dataTwo different unwrap strategies are used deliberately:
unwrap<Json>()beforeprocess— malformed JSON is a hard error; there is nothing useful to pass to the JS functionunwrapOr<Json>(default=|null())afterprocess— a JS runtime error produces a gracefulnullresponse rather than crashing the request
The jsTransform sub-treatment declares its own model engine: Transformer(), which means the model instance is shared with main’s engine through Mélodium’s model propagation — the function is not recompiled per request.
Dependencies
[dependencies]
std = "0.10.1" # core flows, logging, data structures
http = "0.10.1" # HTTP server and client
net = "0.10.1" # IP address helpers
json = "0.10.1" # JSON parsing and serialisation
encoding = "0.10.1" # UTF-8 encode / decode
javascript = "0.10.1" # embedded JavaScript engine