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
package deno
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
const Version = "v2.9.3"
func Resolve(cacheDir string) (string, error) {
return resolve(cacheDir)
}
func resolve(cacheDir string) (string, error) {
if data := EmbeddedZip(); len(data) > 0 {
if path, extractErr := extractEmbedded(cacheDir, data); extractErr == nil {
return path, nil
}
}
if path, lookupErr := exec.LookPath("deno"); lookupErr == nil {
return path, nil
}
return "", errors.New("embedded deno artifact is absent and deno is not in PATH; runtime downloads are disabled")
}
func extractEmbedded(cacheDir string, data []byte) (string, error) {
if cacheDir == "" {
cacheDir = defaultCacheDir()
}
dir := filepath.Join(cacheDir, "deno-embedded", Version)
binaryName := "deno"
if runtime.GOOS == "windows" {
binaryName += ".exe"
}
path := filepath.Join(dir, binaryName)
if info, statErr := os.Stat(path); statErr == nil && !info.IsDir() {
return path, nil
}
if mkdirErr := os.MkdirAll(dir, 0755); mkdirErr != nil {
return "", mkdirErr
}
reader, zipErr := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if zipErr != nil {
return "", zipErr
}
for _, file := range reader.File {
name := filepath.Base(file.Name)
if !strings.HasPrefix(name, "deno") {
continue
}
rc, openErr := file.Open()
if openErr != nil {
return "", openErr
}
out, createErr := os.CreateTemp(dir, name+".*.tmp")
if createErr != nil {
rc.Close()
return "", createErr
}
_, copyErr := io.Copy(out, rc)
closeErr := out.Close()
rc.Close()
if err := errors.Join(copyErr, closeErr, os.Chmod(out.Name(), 0755)); err != nil {
os.Remove(out.Name())
return "", err
}
if err := os.Rename(out.Name(), filepath.Join(dir, name)); err != nil {
os.Remove(out.Name())
return "", err
}
}
os.Chmod(path, 0755)
if info, statErr := os.Stat(path); statErr != nil || info.IsDir() {
return "", fmt.Errorf("deno binary not found after extraction: %s", path)
}
return path, nil
}
func defaultCacheDir() string {
if dir := os.Getenv("XDG_CACHE_HOME"); dir != "" {
return filepath.Join(dir, "policy_engine")
}
if dir, cacheErr := os.UserCacheDir(); cacheErr == nil {
return filepath.Join(dir, "policy_engine")
}
return filepath.Join(os.TempDir(), "policy_engine_cache")
}