-
Notifications
You must be signed in to change notification settings - Fork 847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add javascript
processor
#1406
Add javascript
processor
#1406
Conversation
Hey @kabukky, this is awesome, it might take me a little while to dig into the implementation properly but at a glance it looks great and this is going to be super powerful. |
@Jeffail that is good to hear. Please let me know if you have any questions or suggestions. I will get right to it. Best |
May be add v8go (/~https://github.com/rogchap/v8go) support instead goja?. It's binding for v8 and it's much faster than goja (it's used in current implementation).
In that test v8go x10 times faster than goja. |
@packman80 I'm not sure the performance benefit is worth the hassle of using v8go. A Go native JavaScript VM is much easier to maintain in my opinion. But if the maintainers think otherwise, I will change this to another Go JS library, no problem. |
internal/impl/javascript/vm.go
Outdated
func createRequireRegistry() { | ||
// Initialize global registry. This is where modules (JS files) live. This enables easy code re-use. | ||
// TODO: We need to set a registry root folder. Can we make this configurable somehow? Setting it to the working directory for now. | ||
requireRegistry = require.NewRegistry(require.WithGlobalFolders("")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @kabukky, I still need to put time aside to properly play around with this, but I think the major question I have is whether the registry here could be instanciated per-processor, and how bad the performance hit would be in that case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Jeffail, thanks for looking into this! The performance overhead would be minimal as the require.NewRegistry
call just instantiates a new Struct. I'll change my code.
@kabukky |
@darcyg That's not possible atm. |
I think Of course I use benthos as a rule engine. |
@kennyp My code is on my own gitlab server, so I submit it as a patch. |
@kabukky @Jeffail cat test.yaml
call
mqtt |
@darcyg good job. But can you please issue it as a pull request |
I will deal with it after the release of 4.11.0 a few days later. I'm busy these days. |
Did you get a chance to look at this? Best |
Hey @darcyg, any news ? |
/~https://github.com/darcyg/benthos Sorry, I rarely use github to manage projects. It is unclear how pull requests submit commit to this # 1406. Maybe you can clone my project, modify it and submit it to the main project. I didn't write test code. My go language is temporary learning and not proficient. |
Thanks so much @Jeffail |
Yeah I had a play around with the different options, I've updated the docs to explain how the current system works with caveats. Basically if programs that utilise variables encapsulate their logic within anonymous functions then if I've understood things correctly we should be fine. I've also added examples that show it in action (the structure mutation one here: https://www.benthos.dev/docs/components/processors/javascript#examples). If things turn ugly and it trips up users we can do some tinkering to get it right, but for now I think for the performance benefits it's worth trying this approach. |
You won't remember, but a few months ago I went on discord and asked if a
javascript
processor is something you guys would consider merging into main.I got a very positive reaction, so here it is!
There are a few TODOs in here that are worth discussing I think, so feel free to comment.
Here are two examples:
With inline code:
config.yaml
With .js files:
config.yaml
test.js
foo.js
I've tested this a bit and found JavaScript very helpful when complex logic is needed that is sometimes hard to read or difficult to implement in bloblang.
This is a first draft that works well for me. What do you think?