/
deno.go
375 lines (332 loc) · 9.24 KB
/
deno.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package utils
import (
"archive/zip"
"bytes"
"context"
"crypto/sha256"
"embed"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/go-errors/errors"
"github.com/spf13/afero"
)
var (
//go:embed denos/*
denoEmbedDir embed.FS
// Used by unit tests
DenoPathOverride string
)
const (
DockerDenoDir = "/home/deno"
DockerModsDir = DockerDenoDir + "/modules"
DockerFuncDirPath = DockerDenoDir + "/functions"
)
func GetDenoPath() (string, error) {
if len(DenoPathOverride) > 0 {
return DenoPathOverride, nil
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
denoBinName := "deno"
if runtime.GOOS == "windows" {
denoBinName = "deno.exe"
}
denoPath := filepath.Join(home, ".supabase", denoBinName)
return denoPath, nil
}
func InstallOrUpgradeDeno(ctx context.Context, fsys afero.Fs) error {
denoPath, err := GetDenoPath()
if err != nil {
return err
}
if _, err := fsys.Stat(denoPath); err == nil {
// Upgrade Deno.
cmd := exec.CommandContext(ctx, denoPath, "upgrade", "--version", DenoVersion)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
} else if !errors.Is(err, os.ErrNotExist) {
return err
}
// Install Deno.
if err := MkdirIfNotExistFS(fsys, filepath.Dir(denoPath)); err != nil {
return err
}
// 1. Determine OS triple
assetFilename, err := getDenoAssetFileName()
if err != nil {
return err
}
assetRepo := "denoland/deno"
if runtime.GOOS == "linux" && runtime.GOARCH == "arm64" {
// TODO: version pin to official release once available https://github.com/denoland/deno/issues/1846
assetRepo = "LukeChannings/deno-arm64"
}
// 2. Download & install Deno binary.
{
assetUrl := fmt.Sprintf("https://github.com/%s/releases/download/v%s/%s", assetRepo, DenoVersion, assetFilename)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, assetUrl, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return errors.New("Failed installing Deno binary.")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
r, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
// There should be only 1 file: the deno binary
if len(r.File) != 1 {
return err
}
denoContents, err := r.File[0].Open()
if err != nil {
return err
}
defer denoContents.Close()
denoBytes, err := io.ReadAll(denoContents)
if err != nil {
return err
}
if err := afero.WriteFile(fsys, denoPath, denoBytes, 0755); err != nil {
return err
}
}
return nil
}
func isScriptModified(fsys afero.Fs, destPath string, src []byte) (bool, error) {
dest, err := afero.ReadFile(fsys, destPath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return true, nil
}
return false, err
}
// compare the md5 checksum of src bytes with user's copy.
// if the checksums doesn't match, script is modified.
return sha256.Sum256(dest) != sha256.Sum256(src), nil
}
type DenoScriptDir struct {
ExtractPath string
BuildPath string
}
// Copy Deno scripts needed for function deploy and downloads, returning a DenoScriptDir struct or an error.
func CopyDenoScripts(ctx context.Context, fsys afero.Fs) (*DenoScriptDir, error) {
denoPath, err := GetDenoPath()
if err != nil {
return nil, err
}
denoDirPath := filepath.Dir(denoPath)
scriptDirPath := filepath.Join(denoDirPath, "denos")
// make the script directory if not exist
if err := MkdirIfNotExistFS(fsys, scriptDirPath); err != nil {
return nil, err
}
// copy embed files to script directory
err = fs.WalkDir(denoEmbedDir, "denos", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// skip copying the directory
if d.IsDir() {
return nil
}
destPath := filepath.Join(denoDirPath, path)
contents, err := fs.ReadFile(denoEmbedDir, path)
if err != nil {
return err
}
// check if the script should be copied
modified, err := isScriptModified(fsys, destPath, contents)
if err != nil {
return err
}
if !modified {
return nil
}
if err := afero.WriteFile(fsys, filepath.Join(denoDirPath, path), contents, 0666); err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
sd := DenoScriptDir{
ExtractPath: filepath.Join(scriptDirPath, "extract.ts"),
BuildPath: filepath.Join(scriptDirPath, "build.ts"),
}
return &sd, nil
}
type ImportMap struct {
Imports map[string]string `json:"imports"`
Scopes map[string]map[string]string `json:"scopes"`
}
func NewImportMap(path string, fsys afero.Fs) (*ImportMap, error) {
contents, err := fsys.Open(path)
if err != nil {
return nil, errors.Errorf("failed to load import map: %w", err)
}
defer contents.Close()
return NewFromReader(contents)
}
func NewFromReader(r io.Reader) (*ImportMap, error) {
decoder := json.NewDecoder(r)
importMap := &ImportMap{}
if err := decoder.Decode(importMap); err != nil {
return nil, errors.Errorf("failed to parse import map: %w", err)
}
return importMap, nil
}
func (m *ImportMap) Resolve(fsys afero.Fs) ImportMap {
result := ImportMap{
Imports: make(map[string]string, len(m.Imports)),
Scopes: make(map[string]map[string]string, len(m.Scopes)),
}
for k, v := range m.Imports {
result.Imports[k] = resolveHostPath(v, fsys)
}
for module, mapping := range m.Scopes {
result.Scopes[module] = map[string]string{}
for k, v := range mapping {
result.Scopes[module][k] = resolveHostPath(v, fsys)
}
}
return result
}
func (m *ImportMap) BindModules(resolved ImportMap) []string {
cwd, err := os.Getwd()
if err != nil {
return nil
}
var binds []string
for k, dockerPath := range resolved.Imports {
if strings.HasPrefix(dockerPath, DockerModsDir) {
hostPath := filepath.Join(cwd, FunctionsDir, m.Imports[k])
binds = append(binds, hostPath+":"+dockerPath+":ro,z")
}
}
for module, mapping := range resolved.Scopes {
for k, dockerPath := range mapping {
if strings.HasPrefix(dockerPath, DockerModsDir) {
hostPath := filepath.Join(cwd, FunctionsDir, m.Scopes[module][k])
binds = append(binds, hostPath+":"+dockerPath+":ro,z")
}
}
}
return binds
}
func resolveHostPath(hostPath string, fsys afero.Fs) string {
// All local fs imports will be mounted to /home/deno/modules
if filepath.IsAbs(hostPath) {
return getModulePath(hostPath)
}
rel := filepath.Join(FunctionsDir, hostPath)
exists, err := afero.Exists(fsys, rel)
if err != nil {
logger := GetDebugLogger()
fmt.Fprintln(logger, err)
}
if !exists {
return hostPath
}
if strings.HasPrefix(rel, FunctionsDir) {
suffix := strings.TrimPrefix(rel, FunctionsDir)
return path.Join(DockerFuncDirPath, filepath.ToSlash(suffix))
}
// Directory imports need to be suffixed with /
// Ref: https://deno.com/manual@v1.33.0/basics/import_maps
if strings.HasSuffix(hostPath, string(filepath.Separator)) {
rel += string(filepath.Separator)
}
return getModulePath(rel)
}
func getModulePath(hostPath string) string {
mod := path.Join(DockerModsDir, GetPathHash(hostPath))
if strings.HasSuffix(hostPath, string(filepath.Separator)) {
mod += "/"
} else if ext := filepath.Ext(hostPath); len(ext) > 0 {
mod += ext
}
return mod
}
func GetPathHash(path string) string {
digest := sha256.Sum256([]byte(path))
return hex.EncodeToString(digest[:])
}
func AbsImportMapPath(importMapPath, slug string, fsys afero.Fs) (string, error) {
if importMapPath == "" {
if functionConfig, ok := Config.Functions[slug]; ok && functionConfig.ImportMap != "" {
importMapPath = functionConfig.ImportMap
if !filepath.IsAbs(importMapPath) {
importMapPath = filepath.Join(SupabaseDirPath, importMapPath)
}
} else if exists, err := afero.Exists(fsys, FallbackImportMapPath); exists {
importMapPath = FallbackImportMapPath
} else {
if err != nil {
logger := GetDebugLogger()
fmt.Fprintln(logger, err)
}
return importMapPath, nil
}
}
resolved, err := filepath.Abs(importMapPath)
if err != nil {
return "", err
}
if f, err := fsys.Stat(resolved); err != nil {
return "", errors.Errorf("Failed to read import map: %w", err)
} else if f.IsDir() {
return "", errors.New("Importing directory is unsupported: " + resolved)
}
return resolved, nil
}
func AbsTempImportMapPath(cwd, hostPath string) string {
name := GetPathHash(hostPath) + ".json"
return filepath.Join(cwd, ImportMapsDir, name)
}
func BindImportMap(hostImportMapPath, dockerImportMapPath string, fsys afero.Fs) ([]string, error) {
importMap, err := NewImportMap(hostImportMapPath, fsys)
if err != nil {
return nil, err
}
resolved := importMap.Resolve(fsys)
binds := importMap.BindModules(resolved)
if len(binds) > 0 {
cwd, err := os.Getwd()
if err != nil {
return nil, errors.Errorf("failed to get working directory: %w", err)
}
contents, err := json.MarshalIndent(resolved, "", " ")
if err != nil {
return nil, errors.Errorf("failed to encode json: %w", err)
}
// Rewrite import map to temporary host path
hostImportMapPath = AbsTempImportMapPath(cwd, hostImportMapPath)
if err := WriteFile(hostImportMapPath, contents, fsys); err != nil {
return nil, err
}
}
binds = append(binds, hostImportMapPath+":"+dockerImportMapPath+":ro,z")
return binds, nil
}