1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00
denoland-deno/main.go

227 lines
4.7 KiB
Go
Raw Normal View History

2018-05-13 23:32:01 -04:00
package main
import (
"crypto/md5"
"encoding/hex"
2018-05-14 00:31:48 -04:00
"github.com/golang/protobuf/proto"
2018-05-13 23:32:01 -04:00
"github.com/ry/v8worker2"
2018-05-14 02:50:55 -04:00
"io/ioutil"
2018-05-18 20:39:20 -04:00
"net/http"
2018-05-14 00:31:48 -04:00
"os"
"path"
"runtime"
2018-05-18 20:39:20 -04:00
"strings"
2018-05-18 11:49:28 -04:00
"sync"
"time"
2018-05-13 23:32:01 -04:00
)
2018-05-18 11:49:28 -04:00
var wg sync.WaitGroup
var resChan chan *Msg
func SourceCodeHash(filename string, sourceCodeBuf []byte) string {
h := md5.New()
h.Write([]byte(filename))
h.Write(sourceCodeBuf)
return hex.EncodeToString(h.Sum(nil))
}
2018-05-17 20:53:39 -04:00
func CacheFileName(filename string, sourceCodeBuf []byte) string {
cacheKey := SourceCodeHash(filename, sourceCodeBuf)
return path.Join(CompileDir, cacheKey+".js")
}
2018-05-18 20:39:20 -04:00
func IsRemotePath(filename string) bool {
return strings.HasPrefix(filename, "/$remote$/")
}
func FetchRemoteSource(remotePath string) (buf []byte, err error) {
url := strings.Replace(remotePath, "/$remote$/", "http://", 1)
// println("FetchRemoteSource", url)
res, err := http.Get(url)
if err != nil {
return
}
buf, err = ioutil.ReadAll(res.Body)
//println("FetchRemoteSource", err.Error())
res.Body.Close()
return
}
func HandleSourceCodeFetch(filename string) []byte {
2018-05-17 21:02:06 -04:00
res := &Msg{}
2018-05-18 20:39:20 -04:00
var sourceCodeBuf []byte
var err error
if IsRemotePath(filename) {
sourceCodeBuf, err = FetchRemoteSource(filename)
} else {
sourceCodeBuf, err = Asset("dist/" + filename)
if err != nil {
sourceCodeBuf, err = ioutil.ReadFile(filename)
}
}
if err != nil {
res.Error = err.Error()
} else {
2018-05-17 20:53:39 -04:00
cacheFn := CacheFileName(filename, sourceCodeBuf)
outputCodeBuf, err := ioutil.ReadFile(cacheFn)
var outputCode string
if os.IsNotExist(err) {
outputCode = ""
} else if err != nil {
res.Error = err.Error()
} else {
outputCode = string(outputCodeBuf)
}
res.Payload = &Msg_SourceCodeFetchRes{
SourceCodeFetchRes: &SourceCodeFetchResMsg{
SourceCode: string(sourceCodeBuf),
2018-05-17 20:53:39 -04:00
OutputCode: outputCode,
},
}
}
out, err := proto.Marshal(res)
check(err)
return out
}
2018-05-17 20:53:39 -04:00
func HandleSourceCodeCache(filename string, sourceCode string,
outputCode string) []byte {
fn := CacheFileName(filename, []byte(sourceCode))
outputCodeBuf := []byte(outputCode)
err := ioutil.WriteFile(fn, outputCodeBuf, 0600)
2018-05-17 21:02:06 -04:00
res := &Msg{}
2018-05-17 20:53:39 -04:00
if err != nil {
res.Error = err.Error()
}
out, err := proto.Marshal(res)
check(err)
return out
}
2018-05-18 11:49:28 -04:00
func HandleTimerStart(id int32, interval bool, duration int32) []byte {
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(time.Duration(duration) * time.Millisecond)
resChan <- &Msg{
Payload: &Msg_TimerReady{
TimerReady: &TimerReadyMsg{
Id: id,
},
},
}
}()
return nil
}
func UserHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}
func loadAsset(w *v8worker2.Worker, path string) {
data, err := Asset(path)
check(err)
err = w.Load(path, string(data))
check(err)
}
var DenoDir string
var CompileDir string
var SrcDir string
func createDirs() {
DenoDir = path.Join(UserHomeDir(), ".deno")
CompileDir = path.Join(DenoDir, "compile")
err := os.MkdirAll(CompileDir, 0700)
check(err)
SrcDir = path.Join(DenoDir, "src")
err = os.MkdirAll(SrcDir, 0700)
check(err)
}
func check(e error) {
if e != nil {
panic(e)
}
}
2018-05-14 02:50:55 -04:00
func recv(buf []byte) []byte {
msg := &Msg{}
err := proto.Unmarshal(buf, msg)
check(err)
2018-05-17 21:02:06 -04:00
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)
2018-05-17 21:02:06 -04:00
case *Msg_SourceCodeCache:
payload := msg.GetSourceCodeCache()
2018-05-18 11:49:28 -04:00
return HandleSourceCodeCache(payload.Filename, payload.SourceCode,
payload.OutputCode)
case *Msg_TimerStart:
payload := msg.GetTimerStart()
return HandleTimerStart(payload.Id, payload.Interval, payload.Duration)
2018-05-14 02:50:55 -04:00
default:
panic("Unexpected message")
}
2018-05-14 17:27:34 -04:00
return nil
2018-05-13 23:32:01 -04:00
}
2018-05-14 00:31:48 -04:00
func main() {
2018-05-14 20:57:49 -04:00
args := v8worker2.SetFlags(os.Args)
createDirs()
2018-05-13 23:32:01 -04:00
worker := v8worker2.New(recv)
2018-05-14 00:31:48 -04:00
loadAsset(worker, "dist/main.js")
2018-05-14 13:02:47 -04:00
cwd, err := os.Getwd()
check(err)
2018-05-18 11:49:28 -04:00
resChan = make(chan *Msg)
doneChan := make(chan bool)
2018-05-14 13:02:47 -04:00
out, err := proto.Marshal(&Msg{
Payload: &Msg_Start{
Start: &StartMsg{
Cwd: cwd,
Argv: args,
},
},
2018-05-14 13:02:47 -04:00
})
check(err)
2018-05-14 00:31:48 -04:00
err = worker.SendBytes(out)
if err != nil {
2018-05-14 03:12:36 -04:00
os.Stderr.WriteString(err.Error())
os.Exit(1)
2018-05-14 00:31:48 -04:00
}
2018-05-18 11:49:28 -04:00
// In a goroutine, we wait on for all goroutines to complete (for example
// timers). We use this to signal to the main thread to exit.
go func() {
wg.Wait()
doneChan <- true
}()
for {
select {
case msg := <-resChan:
out, err := proto.Marshal(msg)
err = worker.SendBytes(out)
check(err)
case <-doneChan:
// All goroutines have completed. Now we can exit main().
return
}
}
2018-05-13 23:32:01 -04:00
}