Skip to content

Commit

Permalink
Add ReadCloser method to FS object; implement readCloser and readClos…
Browse files Browse the repository at this point in the history
…erValue functions for improved file handling
  • Loading branch information
trheyi committed Nov 4, 2024
1 parent 76c29da commit f7e7c03
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
15 changes: 12 additions & 3 deletions runtime/v8/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ func jsValueParse(ctx *v8go.Context, value interface{}) (*v8go.Value, error) {
// * | ✅ | object(Uint8Array) | []byte |
// * | ✅ | object | map[string]interface{} |
// * | ✅ | array | []interface{} |
// * | ✅ | object(Promise) | bridge.PromiseT |
// * | ✅ | function | bridge.FunctionT |
// * |-------------------------------------------------------
// * | ✅ | object(Promise) | bridge.PromiseT |
// * | ✅ | function | bridge.FunctionT |
// * |-----------------------------------------------------------
func GoValue(value *v8go.Value, ctx *v8go.Context) (interface{}, error) {

if value.IsNull() {
Expand Down Expand Up @@ -339,6 +339,15 @@ func GoValue(value *v8go.Value, ctx *v8go.Context) (interface{}, error) {
return goValueParse(value, goValue)
}

// YAO External
if value.IsYaoExternal() {
goValue, err := value.External()
if err != nil {
return nil, err
}
return goValue, nil
}

// Map, Array etc.
var goValue interface{}
return goValueParse(value, goValue)
Expand Down
31 changes: 31 additions & 0 deletions runtime/v8/objects/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fs
import (
"encoding/base64"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -80,6 +81,7 @@ func (obj *Object) ExportObject(iso *v8go.Isolate) *v8go.ObjectTemplate {
tmpl.Set("ReadFile", obj.readFile(iso))
tmpl.Set("ReadFileBuffer", obj.readFileBuffer(iso))
tmpl.Set("ReadFileBase64", obj.readFileBase64(iso))
tmpl.Set("ReadCloser", obj.readCloser(iso))
tmpl.Set("WriteFile", obj.writeFile(iso))
tmpl.Set("WriteFileBuffer", obj.writeFileBuffer(iso))
tmpl.Set("WriteFileBase64", obj.writeFileBase64(iso))
Expand Down Expand Up @@ -760,6 +762,27 @@ func (obj *Object) readFileBase64(iso *v8go.Isolate) *v8go.FunctionTemplate {
})
}

func (obj *Object) readCloser(iso *v8go.Isolate) *v8go.FunctionTemplate {
return v8go.NewFunctionTemplate(iso, func(info *v8go.FunctionCallbackInfo) *v8go.Value {
args := info.Args()
if len(args) < 1 {
return obj.errorString(info, "Missing parameters")
}

stor, err := obj.getFS(info)
if err != nil {
return obj.error(info, err)
}

readCloser, err := fs.ReadCloser(stor, args[0].String())
if err != nil {
return obj.error(info, err)
}

return obj.readCloserValue(info, readCloser)
})
}

func (obj *Object) writeFile(iso *v8go.Isolate) *v8go.FunctionTemplate {
return v8go.NewFunctionTemplate(iso, func(info *v8go.FunctionCallbackInfo) *v8go.Value {
args := info.Args()
Expand Down Expand Up @@ -1145,6 +1168,14 @@ func (obj *Object) boolValue(info *v8go.FunctionCallbackInfo, value bool) *v8go.
return res
}

func (obj *Object) readCloserValue(info *v8go.FunctionCallbackInfo, value io.ReadCloser) *v8go.Value {
res, err := v8go.NewExternal(info.Context().Isolate(), value)
if err != nil {
return obj.error(info, err)
}
return res
}

func (obj *Object) arrayBufferValue(info *v8go.FunctionCallbackInfo, value []byte) *v8go.Value {
res, err := bridge.JsValue(info.Context(), value)
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions runtime/v8/objects/fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ func TestFSObjectReadFile(t *testing.T) {
res, err = bridge.GoValue(v, ctx)
assert.True(t, v.IsUint8Array())
assert.Equal(t, data, res)

// ReadCloser
v, err = ctx.RunScript(fmt.Sprintf(`
function ReadCloser() {
var fs = new FS("system")
var hd = fs.ReadCloser("%s");
return hd
}
ReadCloser()
`, f["F1"]), "")

if err != nil {
t.Fatal(err)
}

res, err = bridge.GoValue(v, ctx)
readCloser, ok := res.(*os.File)
if !ok {
t.Fatal("res is not *os.File")
}

err = readCloser.Close()
assert.Nil(t, err)
}

func TestFSObjectRootFs(t *testing.T) {
Expand Down

0 comments on commit f7e7c03

Please sign in to comment.