Skip to content

Commit

Permalink
Merge branch 'readable-stream' of github.com:grafana/k6 into readable…
Browse files Browse the repository at this point in the history
…-stream
  • Loading branch information
joanlopez committed Apr 25, 2024
2 parents 31a2554 + cf4f439 commit 08824fa
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 95 deletions.
3 changes: 3 additions & 0 deletions js/modules/k6/experimental/streams/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const (

// AssertionError is thrown when an assertion fails
AssertionError

// NotSupportedError is thrown when a feature is not supported, or not yet implemented
NotSupportedError
)

type streamError struct {
Expand Down
50 changes: 8 additions & 42 deletions js/modules/k6/experimental/streams/errors_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions js/modules/k6/experimental/streams/goja.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func isNonNegativeNumber(value goja.Value) bool {
}

// setReadOnlyPropertyOf sets a read-only property on the given [goja.Object].
func setReadOnlyPropertyOf(obj *goja.Object, name string, value goja.Value) error {
err := obj.DefineDataProperty(name,
value,
func setReadOnlyPropertyOf(obj *goja.Object, objName, propName string, propValue goja.Value) error {
err := obj.DefineDataProperty(propName,
propValue,
goja.FLAG_FALSE,
goja.FLAG_FALSE,
goja.FLAG_TRUE,
)
if err != nil {
return fmt.Errorf("unable to define %s read-only property on TextEncoder object; reason: %w", name, err)
return fmt.Errorf("unable to define %s read-only property on %s object; reason: %w", propName, objName, err)
}

return nil
Expand Down
16 changes: 9 additions & 7 deletions js/modules/k6/experimental/streams/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (mi *ModuleInstance) NewReadableStream(call goja.ConstructorCall) *goja.Obj

// 4. If underlyingSourceDict["type"] is "bytes":
if underlyingSourceDict.Type == "bytes" {
common.Throw(stream.runtime, newError(RuntimeError, "'bytes' stream is not supported yet"))
common.Throw(stream.runtime, newError(NotSupportedError, "'bytes' stream is not supported yet"))
} else { // 5. Otherwise,
// 5.1. Assert: underlyingSourceDict["type"] does not exist.
if underlyingSourceDict.Type != "" {
Expand Down Expand Up @@ -150,7 +150,8 @@ func (mi *ModuleInstance) initializeStrategy(call goja.ConstructorCall) *goja.Ob
size := runtime.ToValue(defaultSizeFunc)
if len(call.Arguments) > 0 && !common.IsNullish(call.Arguments[0]) {
srcArg := call.Arguments[0].ToObject(runtime)
if !common.IsNullish(srcArg.Get("type")) && srcArg.Get("type").String() == ReadableStreamTypeBytes {
srcTypeArg := srcArg.Get("type")
if !common.IsNullish(srcTypeArg) && srcTypeArg.String() == ReadableStreamTypeBytes {
size = nil
}
}
Expand Down Expand Up @@ -182,27 +183,28 @@ func (mi *ModuleInstance) newCountQueuingStrategy(
size goja.Value,
) *goja.Object {
obj := rt.NewObject()
objName := "CountQueuingStrategy"

if len(call.Arguments) != 1 {
throw(rt, newTypeError(rt, "CountQueuingStrategy takes a single argument"))
throw(rt, newTypeError(rt, objName+" takes a single argument"))
}

if !isObject(call.Argument(0)) {
throw(rt, newTypeError(rt, "CountQueuingStrategy argument must be an object"))
throw(rt, newTypeError(rt, objName+" argument must be an object"))
}

argObj := call.Argument(0).ToObject(rt)
if common.IsNullish(argObj.Get("highWaterMark")) {
throw(rt, newTypeError(rt, "CountQueuingStrategy argument must have 'highWaterMark' property"))
throw(rt, newTypeError(rt, objName+" argument must have 'highWaterMark' property"))
}

highWaterMark := argObj.Get("highWaterMark")
if err := setReadOnlyPropertyOf(obj, "highWaterMark", highWaterMark); err != nil {
if err := setReadOnlyPropertyOf(obj, objName, "highWaterMark", highWaterMark); err != nil {
throw(rt, newTypeError(rt, err.Error()))
}

if !common.IsNullish(size) {
if err := setReadOnlyPropertyOf(obj, "size", size); err != nil {
if err := setReadOnlyPropertyOf(obj, objName, "size", size); err != nil {
throw(rt, newTypeError(rt, err.Error()))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var _ ReadableStreamController = &ReadableStreamDefaultController{}
func NewReadableStreamDefaultControllerObject(controller *ReadableStreamDefaultController) (*goja.Object, error) {
rt := controller.stream.runtime
obj := rt.NewObject()
objName := "ReadableStreamDefaultController"

err := obj.DefineAccessorProperty("desiredSize", rt.ToValue(func() goja.Value {
desiredSize := controller.getDesiredSize()
Expand All @@ -74,21 +75,21 @@ func NewReadableStreamDefaultControllerObject(controller *ReadableStreamDefaultC
}

// Exposing the properties of the [ReadableStreamController] interface
if err := setReadOnlyPropertyOf(obj, "constructor", rt.ToValue(func() goja.Value {
if err := setReadOnlyPropertyOf(obj, objName, "constructor", rt.ToValue(func() goja.Value {
return rt.ToValue(&ReadableStreamDefaultController{})
})); err != nil {
return nil, err
}

if err := setReadOnlyPropertyOf(obj, "close", rt.ToValue(controller.Close)); err != nil {
if err := setReadOnlyPropertyOf(obj, objName, "close", rt.ToValue(controller.Close)); err != nil {
return nil, err
}

if err := setReadOnlyPropertyOf(obj, "enqueue", rt.ToValue(controller.Enqueue)); err != nil {
if err := setReadOnlyPropertyOf(obj, objName, "enqueue", rt.ToValue(controller.Enqueue)); err != nil {
return nil, err
}

if err := setReadOnlyPropertyOf(obj, "error", rt.ToValue(controller.Error)); err != nil {
if err := setReadOnlyPropertyOf(obj, objName, "error", rt.ToValue(controller.Error)); err != nil {
return nil, err
}

Expand Down Expand Up @@ -212,14 +213,14 @@ func (controller *ReadableStreamDefaultController) pullSteps(readRequest ReadReq
}
}

// releaseSteps performs the controller’s steps that run when a reader is
// released, used to clean up reader-specific resources stored in the controller.
// releaseSteps implements the [ReleaseSteps] contract following the default controller's
// [specification].
//
// It implements the ReadableStreamDefaultControllerReleaseSteps [specification]
// algorithm.
//
// [specification]: https://streams.spec.whatwg.org/#readable-stream-default-controller-release-steps
// [ReleaseSteps]: https://streams.spec.whatwg.org/#abstract-opdef-readablestreamcontroller-releasesteps
// [specification]: https://streams.spec.whatwg.org/#abstract-opdef-readablestreamdefaultcontroller-releasesteps
func (controller *ReadableStreamDefaultController) releaseSteps() {
// 1.
return //nolint:gosimple
}

// close implements the [ReadableStreamDefaultControllerClose] algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ReadableStreamDefaultReader struct {
func NewReadableStreamDefaultReaderObject(reader *ReadableStreamDefaultReader) (*goja.Object, error) {
rt := reader.stream.runtime
obj := rt.NewObject()
objName := "ReadableStreamDefaultReader"

err := obj.DefineAccessorProperty("closed", rt.ToValue(func() *goja.Promise {
p, _, _ := reader.GetClosed()
Expand All @@ -27,16 +28,16 @@ func NewReadableStreamDefaultReaderObject(reader *ReadableStreamDefaultReader) (
return nil, err
}

if err := setReadOnlyPropertyOf(obj, "cancel", rt.ToValue(reader.Cancel)); err != nil {
if err := setReadOnlyPropertyOf(obj, objName, "cancel", rt.ToValue(reader.Cancel)); err != nil {
return nil, err
}

// Exposing the properties of the [ReadableStreamDefaultReader] interface
if err := setReadOnlyPropertyOf(obj, "read", rt.ToValue(reader.Read)); err != nil {
if err := setReadOnlyPropertyOf(obj, objName, "read", rt.ToValue(reader.Read)); err != nil {
return nil, err
}

if err := setReadOnlyPropertyOf(obj, "releaseLock", rt.ToValue(reader.ReleaseLock)); err != nil {
if err := setReadOnlyPropertyOf(obj, objName, "releaseLock", rt.ToValue(reader.ReleaseLock)); err != nil {
return nil, err
}

Expand Down
6 changes: 3 additions & 3 deletions js/modules/k6/experimental/streams/readable_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ func (stream *ReadableStream) GetReader(options *goja.Object) goja.Value {
}

// 3. Return ? AcquireReadableStreamBYOBReader(this).
common.Throw(stream.runtime, newError(RuntimeError, "'byob' mode is not supported yet"))
common.Throw(stream.runtime, newError(NotSupportedError, "'byob' mode is not supported yet"))
return goja.Undefined()
}

// Tee implements the [tee] operation.
//
// [tee]: https://streams.spec.whatwg.org/#rs-tee
func (stream *ReadableStream) Tee() goja.Value {
common.Throw(stream.runtime, newError(RuntimeError, "'tee()' is not supported yet"))
common.Throw(stream.runtime, newError(NotSupportedError, "'tee()' is not supported yet"))
return goja.Undefined()
}

Expand Down Expand Up @@ -536,7 +536,7 @@ func (stream *ReadableStream) error(e any) {

// 9. OTHERWISE, reader is a ReadableStreamBYOBReader
// 9.1. Assert: reader implements ReadableStreamBYOBReader.
common.Throw(stream.vu.Runtime(), newError(RuntimeError, "ReadableStreamBYOBReader is not supported yet"))
common.Throw(stream.vu.Runtime(), newError(NotSupportedError, "ReadableStreamBYOBReader is not supported yet"))
}

// fulfillReadRequest implements the [ReadableStreamFulfillReadRequest()] algorithm.
Expand Down
33 changes: 7 additions & 26 deletions js/modulestest/compile.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package modulestest

import (
"io"
"os"
"path"
"path/filepath"
Expand All @@ -10,34 +9,16 @@ import (
)

// CompileFile compiles a JS file as a [*goja.Program].
//
// The base path is used to resolve the file path. The name is the file name.
//
// This function facilitates evaluating javascript test files in a [goja.Runtime] using
// the [goja.Runtime.RunProgram] method.
func CompileFile(base, name string) (*goja.Program, error) {
filename := path.Join(base, name)

//nolint:forbidigo // Allow os.Open in tests
f, err := os.Open(filepath.Clean(filename))
if err != nil {
return nil, err
}
defer func() {
err = f.Close()
if err != nil {
panic(err)
}
}()

b, err := io.ReadAll(f)
if err != nil {
return nil, err
}

return compile(name, b)
}

func compile(name string, b []byte) (*goja.Program, error) {
program, err := goja.Compile(name, string(b), false)
b, err := os.ReadFile(filepath.Clean(path.Join(base, name))) //nolint:forbidigo
if err != nil {
return nil, err
}

return program, nil
return goja.Compile(name, string(b), false)
}

0 comments on commit 08824fa

Please sign in to comment.