summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-05-18 20:39:20 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-05-18 21:25:37 -0400
commit8886e1b55f3495b3b798825274a910e5f231a74b (patch)
tree1b1be7a8819cd24e9a1da9cc1914ff1db34a0b9b /main.go
parent39da69f051c48f15dd4af5e8a4cbe17ff4f349e5 (diff)
Initial support for remote imports
Diffstat (limited to 'main.go')
-rw-r--r--main.go31
1 files changed, 28 insertions, 3 deletions
diff --git a/main.go b/main.go
index 39ba9cb87..c54ce9c38 100644
--- a/main.go
+++ b/main.go
@@ -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()