1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

Clean up msg.proto

This commit is contained in:
Ryan Dahl 2018-05-17 21:02:06 -04:00
parent 4dd46920ee
commit 360c50b52e
5 changed files with 21 additions and 66 deletions

View file

@ -45,7 +45,7 @@ fmt: node_modules
go fmt go fmt
clang-format msg.proto -i clang-format msg.proto -i
test: test: deno
node test.js node test.js
.PHONY: lint clean distclean .PHONY: test lint clean distclean

31
main.go
View file

@ -24,7 +24,7 @@ func CacheFileName(filename string, sourceCodeBuf []byte) string {
} }
func HandleSourceCodeFetch(filename string) []byte { func HandleSourceCodeFetch(filename string) []byte {
res := &Msg{Kind: Msg_SOURCE_CODE_FETCH_RES} res := &Msg{}
sourceCodeBuf, err := Asset("dist/" + filename) sourceCodeBuf, err := Asset("dist/" + filename)
if err != nil { if err != nil {
sourceCodeBuf, err = ioutil.ReadFile(filename) sourceCodeBuf, err = ioutil.ReadFile(filename)
@ -61,7 +61,7 @@ func HandleSourceCodeCache(filename string, sourceCode string,
fn := CacheFileName(filename, []byte(sourceCode)) fn := CacheFileName(filename, []byte(sourceCode))
outputCodeBuf := []byte(outputCode) outputCodeBuf := []byte(outputCode)
err := ioutil.WriteFile(fn, outputCodeBuf, 0600) err := ioutil.WriteFile(fn, outputCodeBuf, 0600)
res := &Msg{Kind: Msg_DATA_RESPONSE} res := &Msg{}
if err != nil { if err != nil {
res.Error = err.Error() res.Error = err.Error()
} }
@ -70,19 +70,6 @@ func HandleSourceCodeCache(filename string, sourceCode string,
return out 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 { func UserHomeDir() string {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
@ -125,15 +112,14 @@ func recv(buf []byte) []byte {
msg := &Msg{} msg := &Msg{}
err := proto.Unmarshal(buf, msg) err := proto.Unmarshal(buf, msg)
check(err) check(err)
switch msg.Kind { switch msg.Payload.(type) {
case Msg_READ_FILE_SYNC: case *Msg_Exit:
return ReadFileSync(msg.Path) payload := msg.GetExit()
case Msg_EXIT: os.Exit(int(payload.Code))
os.Exit(int(msg.Code)) case *Msg_SourceCodeFetch:
case Msg_SOURCE_CODE_FETCH:
payload := msg.GetSourceCodeFetch() payload := msg.GetSourceCodeFetch()
return HandleSourceCodeFetch(payload.Filename) return HandleSourceCodeFetch(payload.Filename)
case Msg_SOURCE_CODE_CACHE: case *Msg_SourceCodeCache:
payload := msg.GetSourceCodeCache() payload := msg.GetSourceCodeCache()
return HandleSourceCodeCache(payload.Filename, payload.SourceCode, payload.OutputCode) return HandleSourceCodeCache(payload.Filename, payload.SourceCode, payload.OutputCode)
default: default:
@ -152,7 +138,6 @@ func main() {
check(err) check(err)
out, err := proto.Marshal(&Msg{ out, err := proto.Marshal(&Msg{
Kind: Msg_START,
Payload: &Msg_Start{ Payload: &Msg_Start{
Start: &StartMsg{ Start: &StartMsg{
Cwd: cwd, Cwd: cwd,

View file

@ -13,8 +13,8 @@ function start(cwd: string, argv: string[]): void {
V8Worker2.recv((ab: ArrayBuffer) => { V8Worker2.recv((ab: ArrayBuffer) => {
const msg = pb.Msg.decode(new Uint8Array(ab)); const msg = pb.Msg.decode(new Uint8Array(ab));
switch (msg.kind) { switch (msg.payload) {
case pb.Msg.MsgKind.START: case "start":
start(msg.start.cwd, msg.start.argv); start(msg.start.cwd, msg.start.argv);
break; break;
default: default:

View file

@ -2,34 +2,15 @@ syntax = "proto3";
package main; package main;
message Msg { message Msg {
enum MsgKind { string error = 1;
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;
oneof payload { oneof payload {
StartMsg start = 90; StartMsg start = 10;
SourceCodeFetchMsg source_code_fetch = 91; SourceCodeFetchMsg source_code_fetch = 11;
SourceCodeFetchResMsg source_code_fetch_res = 92; SourceCodeFetchResMsg source_code_fetch_res = 12;
SourceCodeCacheMsg source_code_cache = 93; 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 // START
@ -50,3 +31,5 @@ message SourceCodeCacheMsg {
string source_code = 2; string source_code = 2;
string output_code = 3; string output_code = 3;
} }
message ExitMsg { int32 code = 1; }

15
os.ts
View file

@ -1,13 +1,11 @@
import { main as pb } from "./msg.pb"; import { main as pb } from "./msg.pb";
import { TextDecoder } from "text-encoding";
// TODO move this to types.ts // TODO move this to types.ts
type TypedArray = Uint8Array | Float32Array | Int32Array; type TypedArray = Uint8Array | Float32Array | Int32Array;
export function exit(code = 0): void { export function exit(code = 0): void {
sendMsgFromObject({ sendMsgFromObject({
kind: pb.Msg.MsgKind.EXIT, exit: { code }
code
}); });
} }
@ -15,7 +13,6 @@ export function sourceCodeFetch(
filename: string filename: string
): { sourceCode: string; outputCode: string } { ): { sourceCode: string; outputCode: string } {
const res = sendMsgFromObject({ const res = sendMsgFromObject({
kind: pb.Msg.MsgKind.SOURCE_CODE_FETCH,
sourceCodeFetch: { filename } sourceCodeFetch: { filename }
}); });
const { sourceCode, outputCode } = res.sourceCodeFetchRes; const { sourceCode, outputCode } = res.sourceCodeFetchRes;
@ -28,21 +25,11 @@ export function sourceCodeCache(
outputCode: string outputCode: string
): void { ): void {
const res = sendMsgFromObject({ const res = sendMsgFromObject({
kind: pb.Msg.MsgKind.SOURCE_CODE_CACHE,
sourceCodeCache: { filename, sourceCode, outputCode } sourceCodeCache: { filename, sourceCode, outputCode }
}); });
throwOnError(res); 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 { function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer {
const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength); const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
return ab as ArrayBuffer; return ab as ArrayBuffer;