diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2018-05-18 20:39:20 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-05-18 21:25:37 -0400 |
commit | 8886e1b55f3495b3b798825274a910e5f231a74b (patch) | |
tree | 1b1be7a8819cd24e9a1da9cc1914ff1db34a0b9b /main.go | |
parent | 39da69f051c48f15dd4af5e8a4cbe17ff4f349e5 (diff) |
Initial support for remote imports
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 31 |
1 files changed, 28 insertions, 3 deletions
@@ -6,9 +6,11 @@ import ( "github.com/golang/protobuf/proto" "github.com/ry/v8worker2" "io/ioutil" + "net/http" "os" "path" "runtime" + "strings" "sync" "time" ) @@ -28,11 +30,34 @@ func CacheFileName(filename string, sourceCodeBuf []byte) string { return path.Join(CompileDir, cacheKey+".js") } +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 { res := &Msg{} - sourceCodeBuf, err := Asset("dist/" + filename) - if err != nil { - sourceCodeBuf, err = ioutil.ReadFile(filename) + 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() |