2024-10-25 19:07:13 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-10-26 17:05:29 +01:00
|
|
|
"git.ailur.dev/ailur/jsStreams"
|
2024-10-25 19:07:13 +01:00
|
|
|
"io"
|
|
|
|
"syscall/js"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NOTE: Please do not use this code as an example. It never closes the stream and will leak memory.
|
|
|
|
// It is intended for use in the developer console, where you can close the stream via JavaScript.
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
js.Global().Set("TryReadStream", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
|
|
go func() {
|
|
|
|
readStream := jsStreams.NewReadableStream(args[0])
|
|
|
|
var buffer []byte
|
|
|
|
buffer, err := io.ReadAll(readStream)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(string(buffer))
|
2024-10-28 10:10:57 +00:00
|
|
|
err = readStream.Close()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2024-10-25 19:07:13 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
js.Global().Set("TryWriteStream", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
|
|
go func() {
|
|
|
|
writeStream := jsStreams.NewWritableStream(args[0])
|
|
|
|
_, err := writeStream.Write([]byte(args[1].String()))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2024-10-28 10:10:57 +00:00
|
|
|
err = writeStream.Close()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2024-10-25 19:07:13 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
2024-10-26 17:05:29 +01:00
|
|
|
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
|
|
|
|
}))
|
|
|
|
|
2024-10-25 19:07:13 +01:00
|
|
|
select {}
|
|
|
|
}
|