1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -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
clang-format msg.proto -i
test:
test: deno
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 {
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,

View file

@ -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:

View file

@ -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; }

15
os.ts
View file

@ -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;