Compare commits
No commits in common. "master" and "v1.1.0" have entirely different histories.
4 changed files with 12 additions and 126 deletions
2
.gitattributes
vendored
2
.gitattributes
vendored
|
@ -1,2 +0,0 @@
|
|||
tests/index.html -linguist-detectable
|
||||
tests/index.html linguist-vendored
|
|
@ -1,8 +1,7 @@
|
|||
# jsStreams
|
||||
|
||||
Go library to communicate with the JS Stream API by bridging the JS ReadableStream and WritableStream objects to a Go io.ReaderCloser and io.WriterCloser.
|
||||
It also works vice versa, and with pipe readers/writers.
|
||||
Go library to communicate with the JS Stream API by bridging the JS ReadableStream object to a Go io.ReaderCloser.
|
||||
|
||||
[](https://goreportcard.com/report/git.ailur.dev/ailur/jsStreams) [](https://pkg.go.dev/git.ailur.dev/ailur/jsStreams)
|
||||
|
||||
The API is pretty self-explanatory, see the Go Reference badge above for the full documentation.
|
||||
The API is pretty self-explanatory - it provides a function to create an io.ReaderCloser from a JS ReadableStream object.
|
63
main.go
63
main.go
|
@ -39,12 +39,11 @@ func (r *ReadableStream) Read(p []byte) (n int, err error) {
|
|||
|
||||
readResult.Call("then", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||||
defer waitGroup.Done()
|
||||
if args[0].Get("done").Bool() || args[0].Get("value").Length() == 0 {
|
||||
err = io.EOF
|
||||
return nil
|
||||
}
|
||||
data := args[0].Get("value")
|
||||
js.CopyBytesToGo(p, data)
|
||||
if args[0].Get("done").Bool() {
|
||||
err = io.EOF
|
||||
}
|
||||
n = data.Length()
|
||||
return nil
|
||||
}))
|
||||
|
@ -67,11 +66,9 @@ func (r *ReadableStream) Close() (err error) {
|
|||
defer func() {
|
||||
// We don't want any errors to be thrown if the stream is already closed.
|
||||
recovery := recover()
|
||||
if !strings.Contains(fmt.Sprint(recovery), "Can not close stream after closing or error") {
|
||||
if recovery != nil {
|
||||
if !strings.Contains(recovery.(string), "Can not close stream after closing or error") {
|
||||
err = fmt.Errorf("panic: %v", recovery)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
r.lock.Lock()
|
||||
|
@ -143,11 +140,9 @@ func (w *WritableStream) Close() (err error) {
|
|||
defer func() {
|
||||
// We don't want any errors to be thrown if the stream is already closed.
|
||||
recovery := recover()
|
||||
if !strings.Contains(fmt.Sprint(recovery), "Can not close stream after closing or error") {
|
||||
if recovery != nil {
|
||||
if !strings.Contains(recovery.(string), "Can not close stream after closing or error") {
|
||||
err = fmt.Errorf("panic: %v", recovery)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
w.lock.Lock()
|
||||
|
@ -167,51 +162,3 @@ func NewWritableStream(stream ...js.Value) *WritableStream {
|
|||
return &WritableStream{stream: stream}
|
||||
}
|
||||
}
|
||||
|
||||
// Now we do the vice versa: Reader to ReadableStream and Writer to WritableStream.
|
||||
|
||||
// ReaderToReadableStream converts an io.Reader to a JavaScript ReadableStream.
|
||||
func ReaderToReadableStream(r io.Reader) js.Value {
|
||||
return js.Global().Get("ReadableStream").New(map[string]interface{}{
|
||||
"pull": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||||
readController := args[0]
|
||||
return js.Global().Get("Promise").New(js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||||
var buffer []byte
|
||||
buffer, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
if len(buffer) == 0 {
|
||||
readController.Call("close")
|
||||
return nil
|
||||
}
|
||||
jsBuffer := js.Global().Get("Uint8Array").New(len(buffer))
|
||||
js.CopyBytesToJS(jsBuffer, buffer)
|
||||
readController.Call("enqueue", jsBuffer)
|
||||
readController.Call("close")
|
||||
args[0].Invoke()
|
||||
return nil
|
||||
}))
|
||||
}),
|
||||
"type": "bytes",
|
||||
})
|
||||
}
|
||||
|
||||
// WriterToWritableStream converts an io.Writer to a JavaScript WritableStream.
|
||||
func WriterToWritableStream(w io.Writer) js.Value {
|
||||
return js.Global().Get("WritableStream").New(map[string]interface{}{
|
||||
"write": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||||
writeBuffer := args[0]
|
||||
return js.Global().Get("Promise").New(js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||||
buffer := make([]byte, writeBuffer.Length())
|
||||
js.CopyBytesToGo(buffer, writeBuffer)
|
||||
_, err := w.Write(buffer)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
args[0].Invoke()
|
||||
return nil
|
||||
}))
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.ailur.dev/ailur/jsStreams"
|
||||
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"syscall/js"
|
||||
)
|
||||
|
||||
|
@ -21,10 +23,6 @@ func main() {
|
|||
return
|
||||
}
|
||||
fmt.Println(string(buffer))
|
||||
err = readStream.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
|
@ -38,62 +36,6 @@ func main() {
|
|||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = writeStream.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}))
|
||||
|
||||
js.Global().Set("TryWriterConversions", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||||
go func() {
|
||||
reader, writer := io.Pipe()
|
||||
go func() {
|
||||
writeStream := jsStreams.WriterToWritableStream(writer)
|
||||
buffer := js.Global().Get("Uint8Array").New(45)
|
||||
js.CopyBytesToJS(buffer, []byte("Hi, I've been piped through a WritableStream!"))
|
||||
writeStream.Call("getWriter").Call("write", buffer).Call("then", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||||
go func() {
|
||||
err := writer.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}))
|
||||
}()
|
||||
go func() {
|
||||
fmt.Println("Reading stream...")
|
||||
m, _ := io.ReadAll(reader)
|
||||
fmt.Println(string(m))
|
||||
}()
|
||||
}()
|
||||
|
||||
return nil
|
||||
}))
|
||||
|
||||
js.Global().Set("TryReaderConversions", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||||
go func() {
|
||||
reader, writer := io.Pipe()
|
||||
go func() {
|
||||
_, err := writer.Write([]byte("Hi, I've been piped through a ReadableStream!"))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = writer.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
fmt.Println("Reading stream...")
|
||||
m, _ := io.ReadAll(jsStreams.NewReadableStream(jsStreams.ReaderToReadableStream(reader)))
|
||||
fmt.Println(string(m))
|
||||
}()
|
||||
}()
|
||||
|
||||
return nil
|
||||
|
|
Loading…
Add table
Reference in a new issue