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

Enable caching

This commit is contained in:
Ryan Dahl 2018-05-17 20:53:39 -04:00
parent 05672b7e24
commit 4dd46920ee

36
main.go
View file

@ -18,6 +18,11 @@ func SourceCodeHash(filename string, sourceCodeBuf []byte) string {
return hex.EncodeToString(h.Sum(nil)) return hex.EncodeToString(h.Sum(nil))
} }
func CacheFileName(filename string, sourceCodeBuf []byte) string {
cacheKey := SourceCodeHash(filename, sourceCodeBuf)
return path.Join(CompileDir, cacheKey+".js")
}
func HandleSourceCodeFetch(filename string) []byte { func HandleSourceCodeFetch(filename string) []byte {
res := &Msg{Kind: Msg_SOURCE_CODE_FETCH_RES} res := &Msg{Kind: Msg_SOURCE_CODE_FETCH_RES}
sourceCodeBuf, err := Asset("dist/" + filename) sourceCodeBuf, err := Asset("dist/" + filename)
@ -27,13 +32,21 @@ func HandleSourceCodeFetch(filename string) []byte {
if err != nil { if err != nil {
res.Error = err.Error() res.Error = err.Error()
} else { } else {
cacheKey := SourceCodeHash(filename, sourceCodeBuf) cacheFn := CacheFileName(filename, sourceCodeBuf)
println("cacheKey", filename, cacheKey) outputCodeBuf, err := ioutil.ReadFile(cacheFn)
// TODO For now don't do any cache lookups.. var outputCode string
if os.IsNotExist(err) {
outputCode = ""
} else if err != nil {
res.Error = err.Error()
} else {
outputCode = string(outputCodeBuf)
}
res.Payload = &Msg_SourceCodeFetchRes{ res.Payload = &Msg_SourceCodeFetchRes{
SourceCodeFetchRes: &SourceCodeFetchResMsg{ SourceCodeFetchRes: &SourceCodeFetchResMsg{
SourceCode: string(sourceCodeBuf), SourceCode: string(sourceCodeBuf),
OutputCode: "", OutputCode: outputCode,
}, },
} }
} }
@ -42,8 +55,19 @@ func HandleSourceCodeFetch(filename string) []byte {
return out return out
} }
func HandleSourceCodeCache(filename string, sourceCode string, outputCode string) []byte { func HandleSourceCodeCache(filename string, sourceCode string,
return nil outputCode string) []byte {
fn := CacheFileName(filename, []byte(sourceCode))
outputCodeBuf := []byte(outputCode)
err := ioutil.WriteFile(fn, outputCodeBuf, 0600)
res := &Msg{Kind: Msg_DATA_RESPONSE}
if err != nil {
res.Error = err.Error()
}
out, err := proto.Marshal(res)
check(err)
return out
} }
func ReadFileSync(filename string) []byte { func ReadFileSync(filename string) []byte {