Skip to content

Commit

Permalink
Feature/transformstream (#158)
Browse files Browse the repository at this point in the history
* improve xmlhttprequest , support transformstream

* add flush func, correct unit test
  • Loading branch information
realPy authored Oct 31, 2024
1 parent d0843d5 commit 94b6a15
Show file tree
Hide file tree
Showing 12 changed files with 324 additions and 40 deletions.
9 changes: 9 additions & 0 deletions base/baseobject/baseobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ func NewFromJSObject(obj js.Value) (BaseObject, error) {

}

// NewEmptyObject empty object.
func NewEmptyObject() (BaseObject, error) {
var o BaseObject
obj := js.ValueOf(map[string]interface{}{})
o.object = &obj
return o, nil

}

// Empty check if the struct is an empty Struct or have a JS Value attached
func (b BaseObject) Empty() bool {

Expand Down
2 changes: 0 additions & 2 deletions base/serviceworkerregistration/serviceworkerregistration.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package serviceworkerregistration

import (
"fmt"
"sync"
"syscall/js"

Expand Down Expand Up @@ -82,7 +81,6 @@ func (s ServiceWorkerRegistration) getserviceworkerAttribute(attribute string) (
return sw, baseobject.ErrUndefinedValue
} else {
if sw, ok = obj.(serviceworker.ServiceWorker); !ok {
fmt.Printf("-->%v\n", obj.(baseobject.BaseObject).ConstructName_())
err = serviceworker.ErrNotAServiceWorker

}
Expand Down
12 changes: 8 additions & 4 deletions base/stream/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package stream
import "errors"

var (
ErrNotImplementedReadableStream = errors.New("Browser not implemented ReadableStream")
ErrNotImplementedWritableStream = errors.New("Browser not implemented WritableStream")
ErrNotAReadableStream = errors.New("Object is not a ReadableStream")
ErrNotAWritableStream = errors.New("Object is not a WritableStream")
ErrNotImplementedReadableStream = errors.New("Browser not implemented ReadableStream")
ErrNotImplementedWritableStream = errors.New("Browser not implemented WritableStream")
ErrNotImplementedTransformStream = errors.New("Browser not implemented TransformStream")
ErrNotAReadableStream = errors.New("Object is not a ReadableStream")
ErrNotAWritableStream = errors.New("Object is not a WritableStream")
ErrNotATransformStream = errors.New("Object is not a TransformStream")
ErrNotAReadableStreamDefaultReader = errors.New("Object is not a ReadableStreamDefaultReader")
ErrNotATransformStreamDefaultController = errors.New("Object is not a TransformStreamDefaultController")
)
3 changes: 2 additions & 1 deletion base/stream/readablestream.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ func init() {

initinterface.RegisterInterface(GetRInterface)
initinterface.RegisterInterface(GetWInterface)
initinterface.RegisterInterface(GetTInterface)
initinterface.RegisterInterface(GetReadableStreamDefaultReaderInterface)
initinterface.RegisterInterface(GetWritableStreamDefaultWriterInterface)

initinterface.RegisterInterface(GetTransformStreamDefaultControllerInterface)
}

var singletonr sync.Once
Expand Down
2 changes: 1 addition & 1 deletion base/stream/readablestreamdefaultreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewReadableStreamDefaultReaderFromJSObject(obj js.Value) (ReadableStreamDef
}
}

return r, ErrNotAReadableStream
return r, ErrNotAReadableStreamDefaultReader
}

func (r ReadableStreamDefaultReader) newRead(data []byte, dataHandle func([]byte, int)) *promise.Promise {
Expand Down
115 changes: 114 additions & 1 deletion base/stream/transformstream.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,122 @@
package stream

import "github.com/realPy/hogosuru/base/baseobject"
import (
"sync"
"syscall/js"

"github.com/realPy/hogosuru/base/baseobject"
)

type TransformStream struct {
baseobject.BaseObject
start, transform, flush js.Func
}

var singletont sync.Once

var transformstreaminterface js.Value

// GetRInterface get the JS interface ReadableStream.
func GetTInterface() js.Value {

singletont.Do(func() {

var err error
if transformstreaminterface, err = baseobject.Get(js.Global(), "TransformStream"); err != nil {
transformstreaminterface = js.Undefined()
}
baseobject.Register(transformstreaminterface, func(v js.Value) (interface{}, error) {
return NewTransformStreamFromJSObject(v)
})
})

return transformstreaminterface
}

// NewReadableStream Create a new ReadableStream
func NewTransformStream(
start func(controller TransformStreamDefaultController),
transform func(chunk interface{}, controller TransformStreamDefaultController),
flush func(controller TransformStreamDefaultController),
) (TransformStream, error) {
var t TransformStream
var obj js.Value
var err error
if ri := GetTInterface(); !ri.IsUndefined() {
ob, _ := baseobject.NewEmptyObject()
t.start = js.FuncOf(func(this js.Value, args []js.Value) interface{} {

if c, err := baseobject.Discover(args[0]); err == nil {
if ctrl, ok := c.(TransformStreamDefaultController); ok && start != nil {
start(ctrl)
}
}
return nil
})
t.transform = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if c, err := baseobject.Discover(args[1]); err == nil {
if ctrl, ok := c.(TransformStreamDefaultController); ok && transform != nil {
chunk, _ := baseobject.Discover(args[0])
transform(chunk, ctrl)

}
}

return nil
})

t.flush = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if c, err := baseobject.Discover(args[0]); err == nil {
if ctrl, ok := c.(TransformStreamDefaultController); ok && flush != nil {
flush(ctrl)

}
}
return nil
})

ob.Set("start", t.start)
ob.Set("transform", t.transform)
ob.Set("flush", t.flush)

if obj, err = baseobject.New(ri, ob.JSObject()); err == nil {
t.BaseObject = t.BaseObject.SetObject(obj)

}

} else {
err = ErrNotImplementedReadableStream
}
return t, err
}

func NewTransformStreamFromJSObject(obj js.Value) (TransformStream, error) {
var t TransformStream
var err error
if tsi := GetTInterface(); !tsi.IsUndefined() {
if obj.IsUndefined() || obj.IsNull() {
err = baseobject.ErrUndefinedValue
} else {

if obj.InstanceOf(tsi) {
t.BaseObject = t.SetObject(obj)

} else {
err = ErrNotATransformStream
}
}
} else {
err = ErrNotImplementedTransformStream
}

return t, err
}

func (t *TransformStream) Release() {

t.start.Release()
t.transform.Release()
t.flush.Release()
}

func TransfertToTransformStream(b baseobject.BaseObject) TransformStream {
Expand Down
85 changes: 85 additions & 0 deletions base/stream/transformstreamdefaultcontroller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package stream

import (
"sync"
"syscall/js"

"github.com/realPy/hogosuru/base/baseobject"
)

var singletonTransformStreamDefaultController sync.Once

var transformstreamdefaultcontrollerinterface js.Value

// GetTransformStreamDefaultControllerInterface
func GetTransformStreamDefaultControllerInterface() js.Value {
singletonTransformStreamDefaultController.Do(func() {

var err error
if transformstreamdefaultcontrollerinterface, err = baseobject.Get(js.Global(), "TransformStreamDefaultController"); err != nil {
transformstreamdefaultcontrollerinterface = js.Undefined()
}

baseobject.Register(transformstreamdefaultcontrollerinterface, func(v js.Value) (interface{}, error) {
return NewTransformStreamDefaultControllerFromJSObject(v)
})
})

return transformstreamdefaultcontrollerinterface
}

type TransformStreamDefaultController struct {
baseobject.BaseObject
}

type TransformStreamDefaultControllerFrom interface {
TransformStreamDefaultController_() TransformStreamDefaultController
}

func (t TransformStreamDefaultController) TransformStreamDefaultController_() TransformStreamDefaultController {
return t
}

func NewTransformStreamDefaultControllerFromJSObject(obj js.Value) (TransformStreamDefaultController, error) {
var t TransformStreamDefaultController
var err error
if wsi := GetTransformStreamDefaultControllerInterface(); !wsi.IsUndefined() {
if obj.IsUndefined() || obj.IsNull() {
err = baseobject.ErrUndefinedValue
} else {

if obj.InstanceOf(wsi) {
t.BaseObject = t.SetObject(obj)

} else {
err = ErrNotATransformStreamDefaultController
}
}
} else {
err = ErrNotImplementedTransformStream
}

return t, err
}

func (t TransformStreamDefaultController) Enqueue(chunk baseobject.BaseObject) error {
var err error

if _, err = t.Call("enqueue", chunk.JSObject()); err != nil {
return err

}

return nil
}

func (t TransformStreamDefaultController) Terminate() error {
var err error

if _, err = t.Call("terminate"); err != nil {
return err

}

return nil
}
1 change: 0 additions & 1 deletion base/stream/writablestreamdefaultwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var writeablestreamdefaultinterface js.Value

// GetWritableStreamDefaultWriterInterface
func GetWritableStreamDefaultWriterInterface() js.Value {

singletonReadableStreamDefault.Do(func() {

var err error
Expand Down
2 changes: 0 additions & 2 deletions base/typedarray/int8array.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ func NewInt8Array(value interface{}) (Int8Array, error) {
}

func NewInt8ArrayFrom(iterable interface{}) (Int8Array, error) {

arr, err := newTypedArrayFrom(GetInt8ArrayInterface(), func(v js.Value) (interface{}, error) {
return NewInt8FromJSObject(v)
}, iterable)
return arr.(Int8Array), err

}

func NewInt8ArrayOf(values ...interface{}) (Int8Array, error) {
Expand Down
1 change: 0 additions & 1 deletion base/typedarray/typedarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

func init() {

initinterface.RegisterInterface(GetFloat32ArrayInterface)
initinterface.RegisterInterface(GetFloat64ArrayInterface)
initinterface.RegisterInterface(GetInt8ArrayInterface)
Expand Down
24 changes: 24 additions & 0 deletions base/xmlhttprequest/xmlhttprequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,30 @@ func (x XMLHTTPRequest) GetResponseHeader(header string) (string, error) {
return "", err
}

// GetAllResponseHeader https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders
func (x XMLHTTPRequest) GetAllResponseHeader() (string, error) {
var responseHeader js.Value
var err error
if responseHeader, err = x.Call("getAllResponseHeaders"); err == nil {

if responseHeader.Type() == js.TypeString {
return responseHeader.String(), nil
} else {
return "", baseobject.ErrObjectNotString
}

}
return "", err
}

// overrideMimeType https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/overrideMimeType
func (x XMLHTTPRequest) OverrideMimeType(mimetype string) error {
var err error
_, err = x.Call("overrideMimeType", js.ValueOf(mimetype))

return err
}

// Response
func (x XMLHTTPRequest) Response() (js.Value, error) {
return x.Get("response")
Expand Down
Loading

0 comments on commit 94b6a15

Please sign in to comment.