k6 icon indicating copy to clipboard operation
k6 copied to clipboard

Support to init a ReadableStream from a io.Reader

Open joanlopez opened this issue 1 year ago • 1 comments

What?

It exposes a mechanism from streams (Go) package to initialize a new ReadableStream from a io.Reader:

  • NewReadableStreamForReader(vu modules.VU, reader io.Reader) *goja.Object

Why?

Because this would makes it possible to reuse the ReadableStream implementation added in v0.51.0 from Go code, so other pieces in k6 (or any k6 extension) that have a Go io.Reader and want to translate to a ReadableStream, usable from JS code, could it.

Checklist

  • [X] I have performed a self-review of my code.
  • [ ] I have added tests for my changes.
  • [ ] I have run linter locally (make lint) and all checks pass.
  • [ ] I have run tests locally (make tests) and all tests pass.
  • [ ] I have commented on my code, particularly in hard-to-understand areas.

Related PR(s)/Issue(s)

N/A

joanlopez avatar May 14 '24 11:05 joanlopez

I think this is great 🚀 🚀 🚀

This is an idea I wanted to bring up here, but I'm not suggesting we should do it, consider it blocking, nor even arguing this is a good idea: I often find myself creating NewSomething() *goja.Object, and NewSomethingFrom(goja.Value) functions when working in extensions. A while ago, when working in WebCrypto, I experimented with the idea of leveraging Go generics to create From and To generic traits.

However, we ended up not needing generics at the time. I wonder if this idea would make sense in such a context, where for a type T, we can implement a trait To[T] *goja.Object and From(goja.Value) *T.

This is somewhat blurry in my head, but I keep thinking about it. Maybe this is something we could try in the future, although I'm not yet certain if it would have benefits beyond creating a contract for creating from and exporting as object given types (and I might be biased by Rust, where this would likely be the way to go in a lot of situations).

I don't know, but if you have thoughts, I'd love to hear them 😄

oleiade avatar May 14 '24 14:05 oleiade