forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os.go
196 lines (173 loc) · 4.57 KB
/
os.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package deno
import (
"github.com/golang/protobuf/proto"
"github.com/spf13/afero"
"io/ioutil"
"net/url"
"os"
"path"
"strings"
)
const assetPrefix string = "/$asset$/"
var fs afero.Fs
func InitOS() {
if Perms.FsWrite {
assert(Perms.FsRead, "Write access requires read access.")
fs = afero.NewOsFs()
} else if Perms.FsRead {
fs = afero.NewReadOnlyFs(afero.NewOsFs())
} else {
panic("Not implemented.")
}
Sub("os", func(buf []byte) []byte {
msg := &Msg{}
check(proto.Unmarshal(buf, msg))
switch msg.Command {
case Msg_CODE_FETCH:
return HandleCodeFetch(
msg.CodeFetchModuleSpecifier,
msg.CodeFetchContainingFile)
case Msg_CODE_CACHE:
return HandleCodeCache(
msg.CodeCacheFilename,
msg.CodeCacheSourceCode,
msg.CodeCacheOutputCode)
case Msg_EXIT:
os.Exit(int(msg.ExitCode))
case Msg_READ_FILE_SYNC:
return ReadFileSync(msg.ReadFileSyncFilename)
case Msg_WRITE_FILE_SYNC:
return WriteFileSync(msg.WriteFileSyncFilename, msg.WriteFileSyncData,
msg.WriteFileSyncPerm)
default:
panic("[os] Unexpected message " + string(buf))
}
return nil
})
}
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
}
}
func ResolveModule(moduleSpecifier string, containingFile string) (
moduleName string, filename string, err error) {
logDebug("os.go ResolveModule moduleSpecifier %s containingFile %s",
moduleSpecifier, containingFile)
containingFile = SrcFileToUrl(containingFile)
moduleSpecifier = SrcFileToUrl(moduleSpecifier)
logDebug("os.go ResolveModule after moduleSpecifier %s containingFile %s",
moduleSpecifier, containingFile)
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()
if resolved.IsAbs() {
filename = path.Join(SrcDir, resolved.Host, resolved.Path)
} else {
filename = resolved.Path
}
return
}
func HandleCodeFetch(moduleSpecifier string, containingFile string) (out []byte) {
assert(moduleSpecifier != "", "moduleSpecifier shouldn't be empty")
res := &Msg{}
var sourceCodeBuf []byte
var err error
defer func() {
if err != nil {
res.Error = err.Error()
}
out, err = proto.Marshal(res)
check(err)
}()
moduleName, filename, err := ResolveModule(moduleSpecifier, containingFile)
if err != nil {
return
}
logDebug("CodeFetch moduleName %s moduleSpecifier %s containingFile %s filename %s",
moduleName, moduleSpecifier, containingFile, filename)
if isRemote(moduleName) {
sourceCodeBuf, err = FetchRemoteSource(moduleName, filename)
} else if strings.HasPrefix(moduleName, assetPrefix) {
f := strings.TrimPrefix(moduleName, assetPrefix)
sourceCodeBuf, err = Asset("dist/" + f)
if err != nil {
logDebug("%s Asset doesn't exist. Return without error", moduleName)
err = nil
}
} else {
assert(moduleName == filename,
"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
}
var sourceCode = string(sourceCodeBuf)
var command = Msg_CODE_FETCH_RES
res = &Msg{
Command: command,
CodeFetchResModuleName: moduleName,
CodeFetchResFilename: filename,
CodeFetchResSourceCode: sourceCode,
CodeFetchResOutputCode: outputCode,
}
return
}
func HandleCodeCache(filename string, sourceCode string,
outputCode string) []byte {
fn := CacheFileName(filename, []byte(sourceCode))
outputCodeBuf := []byte(outputCode)
err := ioutil.WriteFile(fn, outputCodeBuf, 0600)
res := &Msg{}
if err != nil {
res.Error = err.Error()
}
out, err := proto.Marshal(res)
check(err)
return out
}
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
}
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
}