2018-05-28 21:50:44 -04:00
|
|
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
|
|
|
// All rights reserved. MIT License.
|
2018-05-29 04:28:32 -04:00
|
|
|
package deno
|
2018-05-19 05:53:29 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/golang/protobuf/proto"
|
2018-05-27 12:49:20 -04:00
|
|
|
"github.com/spf13/afero"
|
2018-05-19 05:53:29 -04:00
|
|
|
"io/ioutil"
|
2018-05-23 11:09:38 -04:00
|
|
|
"net/url"
|
2018-05-19 05:53:29 -04:00
|
|
|
"os"
|
2018-05-23 11:09:38 -04:00
|
|
|
"path"
|
2018-05-19 05:53:29 -04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const assetPrefix string = "/$asset$/"
|
|
|
|
|
2018-05-27 12:49:20 -04:00
|
|
|
var fs afero.Fs
|
|
|
|
|
2018-05-21 23:00:36 -04:00
|
|
|
func InitOS() {
|
2018-05-27 12:49:20 -04:00
|
|
|
fs = afero.NewOsFs()
|
|
|
|
|
2018-05-21 22:07:40 -04:00
|
|
|
Sub("os", func(buf []byte) []byte {
|
|
|
|
msg := &Msg{}
|
|
|
|
check(proto.Unmarshal(buf, msg))
|
2018-05-25 15:36:13 -04:00
|
|
|
switch msg.Command {
|
2018-05-25 17:21:06 -04:00
|
|
|
case Msg_CODE_FETCH:
|
|
|
|
return HandleCodeFetch(
|
|
|
|
msg.CodeFetchModuleSpecifier,
|
|
|
|
msg.CodeFetchContainingFile)
|
|
|
|
case Msg_CODE_CACHE:
|
|
|
|
return HandleCodeCache(
|
|
|
|
msg.CodeCacheFilename,
|
|
|
|
msg.CodeCacheSourceCode,
|
|
|
|
msg.CodeCacheOutputCode)
|
2018-05-25 15:36:13 -04:00
|
|
|
case Msg_EXIT:
|
|
|
|
os.Exit(int(msg.ExitCode))
|
2018-05-27 12:49:20 -04:00
|
|
|
case Msg_READ_FILE_SYNC:
|
|
|
|
return ReadFileSync(msg.ReadFileSyncFilename)
|
2018-05-28 14:39:02 -04:00
|
|
|
case Msg_WRITE_FILE_SYNC:
|
|
|
|
return WriteFileSync(msg.WriteFileSyncFilename, msg.WriteFileSyncData,
|
|
|
|
msg.WriteFileSyncPerm)
|
2018-05-21 22:07:40 -04:00
|
|
|
default:
|
|
|
|
panic("[os] Unexpected message " + string(buf))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2018-05-19 05:53:29 -04:00
|
|
|
}
|
|
|
|
|
2018-05-28 09:48:33 -04:00
|
|
|
func SrcFileToUrl(filename string) string {
|
|
|
|
assert(len(SrcDir) > 0, "SrcDir shouldn't be empty")
|
|
|
|
if strings.HasPrefix(filename, SrcDir) {
|
|
|
|
rest := strings.TrimPrefix(filename, SrcDir)
|
|
|
|
if rest[0] == '/' {
|
|
|
|
rest = rest[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return "http://" + rest
|
|
|
|
} else {
|
|
|
|
return filename
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 11:09:38 -04:00
|
|
|
func ResolveModule(moduleSpecifier string, containingFile string) (
|
|
|
|
moduleName string, filename string, err error) {
|
2018-05-23 13:23:29 -04:00
|
|
|
|
2018-05-28 09:48:33 -04:00
|
|
|
logDebug("os.go ResolveModule moduleSpecifier %s containingFile %s",
|
|
|
|
moduleSpecifier, containingFile)
|
|
|
|
|
|
|
|
containingFile = SrcFileToUrl(containingFile)
|
|
|
|
moduleSpecifier = SrcFileToUrl(moduleSpecifier)
|
|
|
|
|
|
|
|
// Hack: If there is no extension, just add .ts
|
|
|
|
if path.Ext(moduleSpecifier) == "" {
|
|
|
|
moduleSpecifier = moduleSpecifier + ".ts"
|
|
|
|
}
|
|
|
|
|
|
|
|
logDebug("os.go ResolveModule after moduleSpecifier %s containingFile %s",
|
|
|
|
moduleSpecifier, containingFile)
|
2018-05-23 13:23:29 -04:00
|
|
|
|
2018-05-23 11:09:38 -04:00
|
|
|
moduleUrl, err := url.Parse(moduleSpecifier)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
baseUrl, err := url.Parse(containingFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resolved := baseUrl.ResolveReference(moduleUrl)
|
|
|
|
moduleName = resolved.String()
|
2018-05-28 09:48:33 -04:00
|
|
|
if resolved.IsAbs() {
|
2018-05-23 11:09:38 -04:00
|
|
|
filename = path.Join(SrcDir, resolved.Host, resolved.Path)
|
|
|
|
} else {
|
|
|
|
filename = resolved.Path
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:21:06 -04:00
|
|
|
func HandleCodeFetch(moduleSpecifier string, containingFile string) (out []byte) {
|
2018-05-19 05:56:02 -04:00
|
|
|
assert(moduleSpecifier != "", "moduleSpecifier shouldn't be empty")
|
2018-05-19 05:53:29 -04:00
|
|
|
res := &Msg{}
|
|
|
|
var sourceCodeBuf []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2018-05-25 15:30:09 -04:00
|
|
|
res.Error = err.Error()
|
2018-05-19 05:53:29 -04:00
|
|
|
}
|
|
|
|
out, err = proto.Marshal(res)
|
|
|
|
check(err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
moduleName, filename, err := ResolveModule(moduleSpecifier, containingFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-28 09:48:33 -04:00
|
|
|
logDebug("CodeFetch moduleName %s moduleSpecifier %s containingFile %s filename %s",
|
|
|
|
moduleName, moduleSpecifier, containingFile, filename)
|
2018-05-19 05:53:29 -04:00
|
|
|
|
2018-05-19 05:56:02 -04:00
|
|
|
if isRemote(moduleName) {
|
2018-05-19 05:53:29 -04:00
|
|
|
sourceCodeBuf, err = FetchRemoteSource(moduleName, filename)
|
|
|
|
} else if strings.HasPrefix(moduleName, assetPrefix) {
|
|
|
|
f := strings.TrimPrefix(moduleName, assetPrefix)
|
|
|
|
sourceCodeBuf, err = Asset("dist/" + f)
|
2018-05-26 15:21:15 -04:00
|
|
|
if err != nil {
|
|
|
|
logDebug("%s Asset doesn't exist. Return without error", moduleName)
|
|
|
|
err = nil
|
|
|
|
}
|
2018-05-19 05:53:29 -04:00
|
|
|
} else {
|
2018-05-19 05:56:02 -04:00
|
|
|
assert(moduleName == filename,
|
2018-05-19 05:53:29 -04:00
|
|
|
"if a module isn't remote, it should have the same filename")
|
|
|
|
sourceCodeBuf, err = ioutil.ReadFile(moduleName)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
outputCode, err := LoadOutputCodeCache(filename, sourceCodeBuf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-22 14:10:13 -04:00
|
|
|
var sourceCode = string(sourceCodeBuf)
|
2018-05-25 17:21:06 -04:00
|
|
|
var command = Msg_CODE_FETCH_RES
|
2018-05-25 15:24:24 -04:00
|
|
|
res = &Msg{
|
2018-05-25 17:21:06 -04:00
|
|
|
Command: command,
|
|
|
|
CodeFetchResModuleName: moduleName,
|
|
|
|
CodeFetchResFilename: filename,
|
|
|
|
CodeFetchResSourceCode: sourceCode,
|
|
|
|
CodeFetchResOutputCode: outputCode,
|
2018-05-19 05:53:29 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:21:06 -04:00
|
|
|
func HandleCodeCache(filename string, sourceCode string,
|
2018-05-19 05:53:29 -04:00
|
|
|
outputCode string) []byte {
|
|
|
|
|
|
|
|
fn := CacheFileName(filename, []byte(sourceCode))
|
|
|
|
outputCodeBuf := []byte(outputCode)
|
|
|
|
err := ioutil.WriteFile(fn, outputCodeBuf, 0600)
|
|
|
|
res := &Msg{}
|
|
|
|
if err != nil {
|
2018-05-25 15:30:09 -04:00
|
|
|
res.Error = err.Error()
|
2018-05-19 05:53:29 -04:00
|
|
|
}
|
|
|
|
out, err := proto.Marshal(res)
|
|
|
|
check(err)
|
|
|
|
return out
|
|
|
|
}
|
2018-05-27 12:49:20 -04:00
|
|
|
|
|
|
|
func ReadFileSync(filename string) []byte {
|
|
|
|
data, err := afero.ReadFile(fs, filename)
|
|
|
|
res := &Msg{
|
|
|
|
Command: Msg_READ_FILE_SYNC_RES,
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
res.Error = err.Error()
|
|
|
|
} else {
|
|
|
|
res.ReadFileSyncData = data
|
|
|
|
}
|
|
|
|
out, err := proto.Marshal(res)
|
|
|
|
check(err)
|
|
|
|
return out
|
|
|
|
}
|
2018-05-28 14:39:02 -04:00
|
|
|
|
|
|
|
func WriteFileSync(filename string, data []byte, perm uint32) []byte {
|
|
|
|
err := afero.WriteFile(fs, filename, data, os.FileMode(perm))
|
|
|
|
res := &Msg{}
|
|
|
|
if err != nil {
|
|
|
|
res.Error = err.Error()
|
|
|
|
}
|
|
|
|
out, err := proto.Marshal(res)
|
|
|
|
check(err)
|
|
|
|
return out
|
|
|
|
}
|