forked from web-platform-tests/wpt.fyi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
components_handler.go
38 lines (32 loc) · 1.06 KB
/
components_handler.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
// Copyright 2018 The WPT Dashboard Project. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package webapp
import (
"fmt"
"io/ioutil"
"mime"
"net/http"
"path/filepath"
"regexp"
"github.com/gorilla/mux"
)
var packageRegex = regexp.MustCompile(`(import .* from|import) (['"])(@[^/]*/)`)
const packageRegexReplacement = "$1 $2/node_modules/$3"
// componentsHandler loads a /node_modules/ path, and replaces any
// npm package loads in the js file with paths on the host.
func componentsHandler(w http.ResponseWriter, r *http.Request) {
filePath := mux.Vars(r)["path"]
var bytes []byte
var err error
if filePath != "" {
bytes, err = ioutil.ReadFile(fmt.Sprintf("./node_modules/%s", filePath))
}
if err != nil || bytes == nil {
http.Error(w, fmt.Sprintf("Component %s not found", filePath), http.StatusNotFound)
return
}
bytes = packageRegex.ReplaceAll(bytes, []byte(packageRegexReplacement))
w.Header().Add("Content-Type", mime.TypeByExtension(filepath.Ext(filePath)))
w.Write(bytes)
}