Repository navigation
feat: Quickjs4J integration - #1656
Conversation
|
Welcome to the Microcks community! 💖 Thanks and congrats 🎉 for opening your first pull request here! Be sure to follow the pull request template or please update it accordingly. Hope you have a great time there! 🌟 ~~~~~~~~~ 🌟 📢 If you like Microcks, please ⭐ star ⭐ our repo to support it! 🙏 It really helps the project to gain momentum and credibility. It's a small contribution back to the project with a big impact. |
lbroudoux
left a comment
There was a problem hiding this comment.
Implementation is crystal clear and very elegant. I love it!
I have some other comments and concerns but at a more general level so pursing in regular comments in the PR.
| public JsonNode get(String key) { | ||
| return mapper.convertValue(delegate.get(key), JsonNode.class); | ||
| } | ||
|
|
||
| @HostFunction | ||
| public void set(String key, Object value) { | ||
| delegate.put(key, value); | ||
| } |
There was a problem hiding this comment.
I assume this is because of the JS representation of an object, but shouldn't we also convert on the set() method? Should we have a mapper.readTree() or something?
There was a problem hiding this comment.
That's possible, I was unsure about what we need to expose to the JS environment from the requestContext.
When I have a few meaningful examples of RequestContext I'm happy to revisit and iterate on tests to figure out the best user experience.
There was a problem hiding this comment.
went back to the original implementation, rationale:
- we set plain object from Java
- we get the values from JS
There was a problem hiding this comment.
Not really. The script inserts data or facts into the requestContext so that this data can be used later during the response rendering step. So typically, script will compute stuff, insert them in the context and the response will include it.
There was a problem hiding this comment.
oh, ok, do you have an example so that I'm sure I'm testing the right use-case?
There was a problem hiding this comment.
There was a problem hiding this comment.
I think that's ok. The only missing part here is that later on, foo can be reused as a variable in the response template for example:
{
"foo": "{{ foo }}"
}Then the mock controller will inject the request context into the rendering engine to get the final {"foo": "bar"} response.
There was a problem hiding this comment.
Thanks for the context, in this case I believe the current implementation is "good enough" as the value out of the map is the expected one:
https://github.com/microcks/microcks/pull/1656/files#diff-1c89164c15586f6c599cedee5c6ca2c74546b94fa59ced69cb71869b5c0a8fc1R120
e.g. trying to store values as JsonNode produces double quoting of the strings value
|
Hey @andreaTP I've reviewed the code on this PR, and I like it! The approach is very elegant and clear. I think the "code part" of this proposition is really good, so I'll follow up here on more global questions and concerns. Regarding your initial questions:
IMHO, little snippets are the most common way of doing things. This is for mocking purposes so it shouldn't end in a full implementation 😉 That said, having the ability to reuse modules can be nice, and it is actually done (see https://microcks.io/blog/extend-microcks-with-custom-libs/). It requires additional operations, though, like mounting a file volume in the container image or rebuilding a custom image, embedding your modules. Do you confirm we could keep the same principles as those described in this blog post?
Ideally, I think it could be nice to keep
It looks simple and neat to me. I think "should be as familiar as possible to JS/TS devs" is very good guidance. I'd like to keep it as idiomatic as possible.
Yes, that's a requirement. Regarding the above question, and as stated elsewhere, having the ability to use Some of my questions and concerns:
And yes: let's iterate! |
|
Thanks a lot for the in depth answer @lbroudoux , I hope this is a complete enough answer 🤞 :
Ok, agreed, let's keep focusing on code snippets 👍
As far as I understand it there are no limitations here, the JS "library" file can be picked up at runtime and executed dynamically(even in native-mode as we are using a compiled js interpreter).
That's possible, but I don't have a great way to do it efficiently, do you have suggestions?
Cool 👍
Added a demo fetch implementation and usage here.
QuickJs does support
The support of EcmaScript is pretty complete according to the docs, and we already stressed it with biggish chunks of JS.
QuickJs is executed in a Wasm sandbox:
QuickJs4J is, currently ~700Kb of distributable jar containing QuickJs plus the bindings to it.
The expectation is that everything should work out of the box with
Yes, I have just done it here, but I think we should wait a little to stabilize the design before optimizing this aspect (it might look slightly different depending on how things unroll). |
|
Awesome! Thanks for the answers. It's super clear now! |
|
Unfortunately, we got an issue on unit tests; @andreaTP Can you check this one? |
On it 👍 , please don't consider this PR final just yet, with this I'm mostly gathering feedback and will tight it up later on 🙂 |
|
@lbroudoux I'll make soon another iteration or two on this PR to (start) bridge the gap on the subjects we discussed and try to finalize it 🙂 |
a63decf to
11e41eb
Compare
|
Done an iteration on the code, and I think that most things are in place now.
Not fussy on anything here though 🙂 |
02731b7 to
f82161d
Compare
|
Sorry for the noise on the repo, the CI workflows should be fine now 🤞 |
|
|
||
| /** | ||
| * Utility class that holds methods for creating binding environments and evaluation context for a JSR 233 ScriptEngine. | ||
| * @author laurent |
| * @return A JS object with status and text() method | ||
| */ | ||
| @HostFunction | ||
| public Object fetch(String url) { |
There was a problem hiding this comment.
Saw your answers on JavaScript polymorphism and actual fetch() implementation. I agree this one can be completed later on but shouldn't we - at least - make more arguments available to avoid too many fetch*() methods later on and regression on existing consumers?
Maybe a simple signature like:
public Object fetch(String url, String method, String body, Map<String, String> headers) {
// ...
}will do the trick?
There was a problem hiding this comment.
Sounds good, I can implement the proposed signature 👍
There was a problem hiding this comment.
Done, the handling of JSON should likely be improved, but there is a baseline now.
|
Hi @andreaTP thanks for the great submission! No noise here: only good stuff! I have one more question regarding script caching: earlier on, you pointed to a change that was related to JSR233 support (but still a draft PR), and I also see this commit related to JS module. Is there any caching enabled at the time of your latest 0.0.7 release or is it a WIP that is planned? |
I forgot to say it, but I already took care of caching with this change: With the bump to |
Ok! I'm reading https://github.com/roastedroot/quickjs4j/blob/main/core/src/main/java/io/roastedroot/quickjs4j/core/ScriptCache.java and the Wouldn't it be nice to use at least a |
Expecting very small scripts, and the fact that the cache only retains the QuickJs "compiled" version of those I thought it would be a "day 2" issue to clear the cache 😅 |
|
Updated the PR:
I took the opportunity to make things nicer/easier starting in QuickJs4J, hence the version bump 🙂 |
|
Hi 👋 I'm back from vacation and able to iterate again on this when requested. |
|
Hey @andreaTP |
Signed-off-by: andreatp <[email protected]>
Signed-off-by: andreatp <[email protected]>
Signed-off-by: andreatp <[email protected]>
|
Welcome back @lbroudoux and thanks for picking this up again 🙂 Rebased and re-targeted, should be good 👍 |
|
The Sonar check requires access to secrets, it's not going to pass, I opened #1690 to workaround it. |
lbroudoux
left a comment
There was a problem hiding this comment.
Got a re-read and added a final concern / change request.
Signed-off-by: andreatp <[email protected]>
|
@lbroudoux thanks for the support in this initiative 🙂 Should we plan follow ups? On top of mind I have:
|
|
Yes absolutely! Documentation is the top priority IMHO and we should add something like we did for Groovy here: https://microcks.io/documentation/explanations/dispatching/#script-dispatcher. Documentation is living in the JSON support is also more than welcome! It would be awesome if you created an issue for each follow-up idea you have in mind so that we can keep them in sight! Thanks a lot! |
Description
This is a POC to demonstrate one possible approach to have a JavaScript engine to evaluate the script in dispatch.
The current implementation is for demo purposes, early questions:
DispatchStylebut I'm unsure it's a good idea, any preference on how to roll this forward?JsApireasonable? any specific preference for accessing fields etc.? or high level guidance like: "should be as familiar as possible to JS/TS devs"Happy to hear feedback!
Related issue(s)
See also #1559