deno.land / x / esm@v135_2 / server / utils.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
package server
import ( "context" "encoding/base64" "errors" "fmt" "net" "net/http" "os" "path/filepath" "regexp" "strings" "time"
"github.com/Masterminds/semver/v3" "github.com/ije/esbuild-internal/config" "github.com/ije/esbuild-internal/js_ast" "github.com/ije/esbuild-internal/js_parser" "github.com/ije/esbuild-internal/logger")
const EOL = "\n"
var ( regexpFullVersion = regexp.MustCompile(`^\d+\.\d+\.\d+[\w\.\+\-]*$`) regexpFullVersionPath = regexp.MustCompile(`(\w)@(v?\d+\.\d+\.\d+[\w\.\+\-]*|[0-9a-f]{10})(/|$)`) regexpPathWithVersion = regexp.MustCompile(`\w@[\*\~\^\w\.\+\-]+(/|$|&)`) regexpBuildVersionPath = regexp.MustCompile(`^/v\d+(/|$)`) regexpCliPath = regexp.MustCompile(`^/v\d+\/?$`) regexpLocPath = regexp.MustCompile(`(\.js):\d+:\d+$`) regexpJSIdent = regexp.MustCompile(`^[a-zA-Z_$][\w$]*$`) regexpGlobalIdent = regexp.MustCompile(`__[a-zA-Z]+\$`) regexpVarEqual = regexp.MustCompile(`var ([a-zA-Z]+)\s*=\s*[a-zA-Z]+$`))
var httpClient = &http.Client{ Transport: &http.Transport{ Proxy: http.ProxyFromEnvironment, DialContext: transportDialContext(&net.Dialer{ Timeout: 10 * time.Second, KeepAlive: 30 * time.Second, }), ForceAttemptHTTP2: true, MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, ResponseHeaderTimeout: 10 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, },}
func transportDialContext(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error) { return dialer.DialContext}
func fetch(url string) (res *http.Response, err error) { return httpClient.Get(url)}
// isHttpSepcifier returns true if the import path is a remote URL.func isHttpSepcifier(importPath string) bool { return strings.HasPrefix(importPath, "https://") || strings.HasPrefix(importPath, "http://")}
// isLocalSpecifier returns true if the import path is a local path.func isLocalSpecifier(importPath string) bool { return strings.HasPrefix(importPath, "file://") || strings.HasPrefix(importPath, "/") || strings.HasPrefix(importPath, "./") || strings.HasPrefix(importPath, "../") || importPath == "." || importPath == ".."}
func semverLessThan(a string, b string) bool { return semver.MustParse(a).LessThan(semver.MustParse(b))}
// includes returns true if the given string is included in the given array.func includes(a []string, s string) bool { if len(a) == 0 { return false } for _, v := range a { if v == s { return true } } return false}
func filter(a []string, fn func(s string) bool) []string { l := len(a) if l == 0 { return nil } b := make([]string, l) i := 0 for _, v := range a { if fn(v) { b[i] = v i++ } } return b[:i]}
func cloneMap(m map[string]string) map[string]string { n := make(map[string]string, len(m)) for k, v := range m { n[k] = v } return n}
func endsWith(s string, suffixs ...string) bool { for _, suffix := range suffixs { if strings.HasSuffix(s, suffix) { return true } } return false}
func stripModuleExt(s string) string { for _, ext := range jsExts { if strings.HasSuffix(s, ext) { return s[:len(s)-len(ext)] } } return s}
func dirExists(filepath string) bool { fi, err := os.Lstat(filepath) return err == nil && fi.IsDir()}
func fileExists(filepath string) bool { fi, err := os.Lstat(filepath) return err == nil && !fi.IsDir()}
func ensureDir(dir string) (err error) { _, err = os.Lstat(dir) if err != nil && os.IsNotExist(err) { err = os.MkdirAll(dir, 0755) } return}
func findFiles(root string, dir string, fn func(p string) bool) ([]string, error) { rootDir, err := filepath.Abs(root) if err != nil { return nil, err } entries, err := os.ReadDir(rootDir) if err != nil { return nil, err } var files []string for _, entry := range entries { name := entry.Name() path := name if dir != "" { path = dir + "/" + name } if entry.IsDir() { if name == "node_modules" { continue } subFiles, err := findFiles(filepath.Join(rootDir, name), path, fn) if err != nil { return nil, err } n := len(files) files = make([]string, n+len(subFiles)) for i, f := range subFiles { files[i+n] = f } copy(files, subFiles) } else { if fn(path) { files = append(files, path) } } } return files, nil}
func btoaUrl(s string) string { return strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(s)), "=")}
func atobUrl(s string) (string, error) { if l := len(s) % 4; l > 0 { s += strings.Repeat("=", 4-l) } data, err := base64.URLEncoding.DecodeString(s) if err != nil { return "", err } return string(data), nil}
func validateJS(filename string) (isESM bool, namedExports []string, err error) { data, err := os.ReadFile(filename) if err != nil { return } log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug, nil) parserOpts := js_parser.OptionsFromConfig(&config.Options{ TS: config.TSOptions{ Parse: endsWith(filename, ".ts", ".mts", ".cts", ".tsx"), }, }) ast, pass := js_parser.Parse(log, logger.Source{ Index: 0, KeyPath: logger.Path{Text: "<stdin>"}, PrettyPath: "<stdin>", Contents: string(data), IdentifierName: "stdin", }, parserOpts) if !pass { err = errors.New("invalid syntax, require javascript/typescript") return } isESM = ast.ExportsKind == js_ast.ExportsESM namedExports = make([]string, len(ast.NamedExports)) i := 0 for name := range ast.NamedExports { namedExports[i] = name i++ } return}
func removeHttpPrefix(s string) (string, error) { for i, v := range s { if v == ':' { return s[i+1:], nil } } return "", fmt.Errorf("colon not found in string: %s", s)}
func concatBytes(a, b []byte) []byte { c := make([]byte, len(a)+len(b)) copy(c, a) copy(c[len(a):], b) return c}
func jsDataUrl(code string) string { return fmt.Sprintf("data:text/javascript;base64,%s", base64.StdEncoding.EncodeToString([]byte(code)))}
esm

Version Info

Tagged at
2 months ago