grafana / sobek

MIT License
147 stars 1 forks source link

Es module usage #49

Open nickchomey opened 1 month ago

nickchomey commented 1 month ago

Sobek seems extremely promising, but I have one main question about the es module support.

The Readme says

Also of note is that due to the nature of ESM you need to have an event loop implementation to use it. That is still not provided by Sobek and likely never will be.

Does this mean we can't just swap Sobek for Goja in order to use ES modules in a Go app?

Similarly, does it mean that k6 has implemented a particular event loop to allow for this? Could k6 be used within a Go app, rather than just Sobek, to allow for es module support?

Thanks!

mstoykov commented 1 month ago

Hi @nickchomey,

Does this mean we can't just swap Sobek for Goja in order to use ES modules in a Go app?

Even if you swapped it you will still need an event loop. ECMAScript modules get executed quite differently to how scripts do (original PR to goja for scale). This requires that:

  1. You use the API for running modules instead of scripts
  2. which in itself is based on promises and will require you to implement some stuff in order to use it

both of those + the fact that ECMAScript modules are asynchronous by nature make it requirement that you an event loop. It can be a bare minimum one, but you need one.

While in the future we might make a project simialr to goja_nodejs, the current purpose of Sobek is to compliment k6 development.

I would like to pull out some code from k6 and make it generally available for Sobek users to just use it, but this is unfortunately not in the current priority list and likely will require some changes. Some of those changes are already kind of underway for different reasons, so 🤷

Could k6 be used within a Go app, rather than just Sobek, to allow for es module support?

You definitely can run js code using k6, and you might be able to reuse parts of it in another go app, but we are unlikely to make changes to support anything and it is possible we will make breaking changes to the parts you use. Also you might want to check the license as it is AGPL 3.0, which depending on your use case might a problem.

You are probably better off looking into the tests files such as

https://github.com/grafana/sobek/blob/2dc9daf5bfa2535ddba0c5eb4f9e7e587fab7d64/modules_test.go#L295-L377

and

https://github.com/grafana/sobek/blob/main/modules_integration_test.go

While both of those are fairly crude they actually do implement more or less what is in k6, but without a bunch of k6 specific stuff, or just in a very simplistic way that handles errors badly.

p.s. as noted in the readme the current ESM API evolved while being developed and hte specificaiton changes half way through. The api isn't particularly ergonomic and likely will be redone in the future.

nickchomey commented 1 month ago

Thanks very much!

I'll explore the tests you linked to to see if it could be implemented in another tool (the open source Conduit CDC/ETL tool is what I have in mind, but surely sobek would be of interest to many others who want to get ESM support in goja).

As for running it via k6, thanks for pointing out the agpl license. That's, of course, completely fair for you to use. In this particular context, I'd just be using it internally rather than offering it to others, so shouldn't be affected by the need to open source everything. An alternative is, of course, to just run the k6 binary on its own and interact via its cli commands.

Anyway, thanks again for the great response and for all of your work on sobek and k6! I'll be following the progress of both projects, and hopefully even find a way to contribute someday!