From 360c50b52eb886007659a93cc9437adfb5e94ca3 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 17 May 2018 21:02:06 -0400 Subject: [PATCH] Clean up msg.proto --- Makefile | 4 ++-- main.go | 31 ++++++++----------------------- main.ts | 4 ++-- msg.proto | 33 ++++++++------------------------- os.ts | 15 +-------------- 5 files changed, 21 insertions(+), 66 deletions(-) diff --git a/Makefile b/Makefile index fca1c2f8f7..a7ee97ecab 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ fmt: node_modules go fmt clang-format msg.proto -i -test: +test: deno node test.js -.PHONY: lint clean distclean +.PHONY: test lint clean distclean diff --git a/main.go b/main.go index a277f24d62..be7ad541f2 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,7 @@ func CacheFileName(filename string, sourceCodeBuf []byte) string { } func HandleSourceCodeFetch(filename string) []byte { - res := &Msg{Kind: Msg_SOURCE_CODE_FETCH_RES} + res := &Msg{} sourceCodeBuf, err := Asset("dist/" + filename) if err != nil { sourceCodeBuf, err = ioutil.ReadFile(filename) @@ -61,7 +61,7 @@ func HandleSourceCodeCache(filename string, sourceCode string, fn := CacheFileName(filename, []byte(sourceCode)) outputCodeBuf := []byte(outputCode) err := ioutil.WriteFile(fn, outputCodeBuf, 0600) - res := &Msg{Kind: Msg_DATA_RESPONSE} + res := &Msg{} if err != nil { res.Error = err.Error() } @@ -70,19 +70,6 @@ func HandleSourceCodeCache(filename string, sourceCode string, return out } -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) - check(err) - return out -} - func UserHomeDir() string { if runtime.GOOS == "windows" { home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") @@ -125,15 +112,14 @@ func recv(buf []byte) []byte { msg := &Msg{} err := proto.Unmarshal(buf, msg) check(err) - switch msg.Kind { - case Msg_READ_FILE_SYNC: - return ReadFileSync(msg.Path) - case Msg_EXIT: - os.Exit(int(msg.Code)) - case Msg_SOURCE_CODE_FETCH: + switch msg.Payload.(type) { + case *Msg_Exit: + payload := msg.GetExit() + os.Exit(int(payload.Code)) + case *Msg_SourceCodeFetch: payload := msg.GetSourceCodeFetch() return HandleSourceCodeFetch(payload.Filename) - case Msg_SOURCE_CODE_CACHE: + case *Msg_SourceCodeCache: payload := msg.GetSourceCodeCache() return HandleSourceCodeCache(payload.Filename, payload.SourceCode, payload.OutputCode) default: @@ -152,7 +138,6 @@ func main() { check(err) out, err := proto.Marshal(&Msg{ - Kind: Msg_START, Payload: &Msg_Start{ Start: &StartMsg{ Cwd: cwd, diff --git a/main.ts b/main.ts index 4b64650f96..e840e5b5a9 100644 --- a/main.ts +++ b/main.ts @@ -13,8 +13,8 @@ function start(cwd: string, argv: string[]): void { V8Worker2.recv((ab: ArrayBuffer) => { const msg = pb.Msg.decode(new Uint8Array(ab)); - switch (msg.kind) { - case pb.Msg.MsgKind.START: + switch (msg.payload) { + case "start": start(msg.start.cwd, msg.start.argv); break; default: diff --git a/msg.proto b/msg.proto index fbedf9752b..181dcc551e 100644 --- a/msg.proto +++ b/msg.proto @@ -2,34 +2,15 @@ syntax = "proto3"; package main; message Msg { - enum MsgKind { - START = 0; - READ_FILE_SYNC = 1; - DATA_RESPONSE = 2; - EXIT = 3; - - SOURCE_CODE_FETCH = 4; - SOURCE_CODE_FETCH_RES = 5; - SOURCE_CODE_CACHE = 6; - } - MsgKind kind = 10; + string error = 1; oneof payload { - StartMsg start = 90; - SourceCodeFetchMsg source_code_fetch = 91; - SourceCodeFetchResMsg source_code_fetch_res = 92; - SourceCodeCacheMsg source_code_cache = 93; + StartMsg start = 10; + SourceCodeFetchMsg source_code_fetch = 11; + SourceCodeFetchResMsg source_code_fetch_res = 12; + SourceCodeCacheMsg source_code_cache = 13; + ExitMsg exit = 14; } - - // READ_FILE_SYNC and MKDIRP - string path = 20; - - // DATA_RESPONSE - bytes data = 30; - string error = 31; - - // EXIT - int32 code = 40; } // START @@ -50,3 +31,5 @@ message SourceCodeCacheMsg { string source_code = 2; string output_code = 3; } + +message ExitMsg { int32 code = 1; } diff --git a/os.ts b/os.ts index 4952609f21..d09026c6cc 100644 --- a/os.ts +++ b/os.ts @@ -1,13 +1,11 @@ import { main as pb } from "./msg.pb"; -import { TextDecoder } from "text-encoding"; // TODO move this to types.ts type TypedArray = Uint8Array | Float32Array | Int32Array; export function exit(code = 0): void { sendMsgFromObject({ - kind: pb.Msg.MsgKind.EXIT, - code + exit: { code } }); } @@ -15,7 +13,6 @@ export function sourceCodeFetch( filename: string ): { sourceCode: string; outputCode: string } { const res = sendMsgFromObject({ - kind: pb.Msg.MsgKind.SOURCE_CODE_FETCH, sourceCodeFetch: { filename } }); const { sourceCode, outputCode } = res.sourceCodeFetchRes; @@ -28,21 +25,11 @@ export function sourceCodeCache( outputCode: string ): void { const res = sendMsgFromObject({ - kind: pb.Msg.MsgKind.SOURCE_CODE_CACHE, sourceCodeCache: { filename, sourceCode, outputCode } }); throwOnError(res); } -export function readFileSync(filename: string): string { - const res = sendMsgFromObject({ - kind: pb.Msg.MsgKind.READ_FILE_SYNC, - path: filename - }); - const decoder = new TextDecoder("utf8"); - return decoder.decode(res.data); -} - function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer { const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength); return ab as ArrayBuffer;