From aba6a1dc871edbc6cbb286a350a4ba79ca645fb8 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 14 May 2018 02:50:55 -0400 Subject: readFileSync is working --- main.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 3ee79fb6b..4612849db 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,37 @@ package main import ( "github.com/golang/protobuf/proto" "github.com/ry/v8worker2" + "io/ioutil" "os" ) -func recv(msg []byte) []byte { - println("recv cb", string(msg)) - return nil +func ReadFileSync(filename string) []byte { + buf, err := ioutil.ReadFile(filename) + msg := &Msg{Kind: Msg_DATA_RESPONSE} + if err != nil { + msg.Error = err.Error() + } else { + msg.Data = buf + } + out, err := proto.Marshal(msg) + if err != nil { + panic(err) + } + return out +} + +func recv(buf []byte) []byte { + msg := &Msg{} + err := proto.Unmarshal(buf, msg) + if err != nil { + panic(err) + } + switch msg.Kind { + case Msg_READ_FILE_SYNC: + return ReadFileSync(msg.Path) + default: + panic("Unexpected message") + } } func loadAsset(w *v8worker2.Worker, path string) { -- cgit v1.2.3