🌐 AI搜索 & 代理 主页
Skip to content

Commit 751ba01

Browse files
Merge pull request #1394 from Workiva/serveUp1
Worked on gopherjs serve
2 parents 32d7337 + 1c6413d commit 751ba01

File tree

5 files changed

+88
-8
lines changed

5 files changed

+88
-8
lines changed

compiler/compiler.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ func ImportDependencies(archive *Archive, importPkg func(string) (*Archive, erro
121121
return deps, nil
122122
}
123123

124+
// DefaultFilter creates a new sourcemapx.Filter that writes to the given writer.
125+
// This is not in the internal package so that tools like the playground can
126+
// use it when calling things like the exported WriteProgramCode function.
127+
func DefaultFilter(w io.Writer) *sourcemapx.Filter {
128+
return &sourcemapx.Filter{Writer: w}
129+
}
130+
124131
func WriteProgramCode(pkgs []*Archive, w *sourcemapx.Filter, goVersion string) error {
125132
mainPkg := pkgs[len(pkgs)-1]
126133
minify := mainPkg.Minified

compiler/natives/src/runtime/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ func Stack(buf []byte, all bool) int {
448448
if s == js.Undefined {
449449
return 0
450450
}
451-
return copy(buf, s.Call("substr", s.Call("indexOf", "\n").Int()+1).String())
451+
return copy(buf, s.Call("substring", s.Call("indexOf", "\n").Int()+1).String())
452452
}
453453

454454
func LockOSThread() {}

compiler/prelude/prelude.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ if (!$global.fs) {
3838
outputBuf += decoder.decode(buf);
3939
var nl = outputBuf.lastIndexOf("\n");
4040
if (nl != -1) {
41-
console.log(outputBuf.substr(0, nl));
42-
outputBuf = outputBuf.substr(nl + 1);
41+
console.log(outputBuf.substring(0, nl));
42+
outputBuf = outputBuf.substring(nl + 1);
4343
}
4444
return buf.length;
4545
},

compiler/prelude/types.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ var $funcType = (params, results, variadic) => {
565565
if (typ === undefined) {
566566
var paramTypes = $mapArray(params, p => { return p.string; });
567567
if (variadic) {
568-
paramTypes[paramTypes.length - 1] = "..." + paramTypes[paramTypes.length - 1].substr(2);
568+
paramTypes[paramTypes.length - 1] = "..." + paramTypes[paramTypes.length - 1].substring(2);
569569
}
570570
var string = "func(" + paramTypes.join(", ") + ")";
571571
if (results.length === 1) {
@@ -588,7 +588,7 @@ var $interfaceType = methods => {
588588
var string = "interface {}";
589589
if (methods.length !== 0) {
590590
string = "interface { " + $mapArray(methods, m => {
591-
return (m.pkg !== "" ? m.pkg + "." : "") + m.name + m.typ.string.substr(4);
591+
return (m.pkg !== "" ? m.pkg + "." : "") + m.name + m.typ.string.substring(4);
592592
}).join("; ") + " }";
593593
}
594594
typ = $newType(8, $kindInterface, string, false, "", false, null);

tool.go

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,11 +613,27 @@ type serveCommandFileSystem struct {
613613
}
614614

615615
func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
616-
name := path.Join(fs.serveRoot, requestName[1:]) // requestName[0] == '/'
617-
log.Printf("Request: %s", name)
616+
requestName = strings.TrimPrefix(requestName, `/`) // Strip leading '/'
617+
name := path.Join(fs.serveRoot, requestName)
618+
log.WithField(`request`, requestName).
619+
Print(`Request`)
620+
621+
// Check if the file is reachable from the serve root.
622+
if f, err := http.Dir(fs.serveRoot).Open(requestName); err == nil {
623+
log.WithField(`request`, requestName).
624+
Print(`Found in serve root`)
625+
return f, nil
626+
}
627+
628+
if strings.HasPrefix(requestName, `.well-known/`) {
629+
// Ignore ".well-known/" requests since they are probes.
630+
// See https://en.wikipedia.org/wiki/Well-known_URI
631+
return nil, os.ErrNotExist
632+
}
618633

619634
dir, file := path.Split(name)
620-
base := path.Base(dir) // base is parent folder name, which becomes the output file name.
635+
dir = strings.TrimSuffix(dir, `/`) // Strip tailing '/'
636+
base := path.Base(dir) // base is parent folder name, which becomes the output file name.
621637

622638
isPkg := file == base+".js"
623639
isMap := file == base+".js.map"
@@ -627,9 +643,42 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
627643
// TODO(dmitshur): might be possible to get a single session to detect changes to source code on disk
628644
s, err := gbuild.NewSession(fs.options)
629645
if err != nil {
646+
log.WithField(`request`, requestName).WithError(err).
647+
Error(`Failed to creates a session`)
630648
return nil, err
631649
}
632650

651+
// Check if the file is reachable from the Go path.
652+
if f, err := http.Dir(path.Join(s.XContext().Env().GOPATH, `src`)).Open(requestName); err == nil {
653+
log.WithField(`request`, requestName).
654+
Print(`Found in Go path`)
655+
return f, nil
656+
}
657+
658+
// Check if the file is reachable from the Go root.
659+
if f, err := http.Dir(path.Join(s.XContext().Env().GOROOT, `src`)).Open(requestName); err == nil {
660+
log.WithField(`request`, requestName).
661+
Print(`Found in Go root`)
662+
return f, nil
663+
}
664+
665+
// Check if the request's dir is an import path.
666+
if pkg, err := s.XContext().Import(dir, fs.serveRoot, build.FindOnly); err == nil {
667+
f, err := http.Dir(pkg.Dir).Open(file)
668+
if err == nil {
669+
log.WithField(`request`, requestName).
670+
Print(`Found in import path`)
671+
return f, nil
672+
}
673+
674+
if !os.IsNotExist(err) {
675+
log.WithField(`request`, requestName).
676+
WithError(err).
677+
Error(`Failed to open file in import path`)
678+
return nil, err
679+
}
680+
}
681+
633682
if isPkg || isMap || isIndex {
634683
// If we're going to be serving our special files, make sure there's a Go command in this folder.
635684
pkg, err := gbuild.Import(path.Dir(name), 0, s.InstallSuffix(), fs.options.BuildTags)
@@ -646,6 +695,10 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
646695
err := func() error {
647696
archive, err := s.BuildProject(pkg)
648697
if err != nil {
698+
log.WithField(`request`, requestName).
699+
WithField(`package`, pkg.ImportPath).
700+
WithError(err).
701+
Error(`Failed to build project`)
649702
return err
650703
}
651704

@@ -654,9 +707,17 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
654707

655708
deps, err := compiler.ImportDependencies(archive, s.ImportResolverFor(""))
656709
if err != nil {
710+
log.WithField(`request`, requestName).
711+
WithField(`package`, pkg.ImportPath).
712+
WithError(err).
713+
Error(`Failed to import dependencies`)
657714
return err
658715
}
659716
if err := compiler.WriteProgramCode(deps, sourceMapFilter, s.GoRelease()); err != nil {
717+
log.WithField(`request`, requestName).
718+
WithField(`package`, pkg.ImportPath).
719+
WithError(err).
720+
Error(`Failed to write program code`)
660721
return err
661722
}
662723

@@ -671,30 +732,42 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
671732
if err != nil {
672733
buf = browserErrors
673734
}
735+
log.WithField(`request`, requestName).
736+
Print(`Created faked JS file for package`)
674737
return newFakeFile(base+".js", buf.Bytes()), nil
675738

676739
case isMap:
677740
if content, ok := fs.sourceMaps[name]; ok {
741+
log.WithField(`request`, requestName).
742+
Print(`Found source map for faked JS file`)
678743
return newFakeFile(base+".js.map", content), nil
679744
}
680745
}
681746
}
682747

683748
// First try to serve the request with a root prefix supplied in the CLI.
684749
if f, err := fs.serveSourceTree(s.XContext(), name); err == nil {
750+
log.WithField(`request`, requestName).
751+
Print(`Found with root prefix`)
685752
return f, nil
686753
}
687754

688755
// If that didn't work, try without the prefix.
689756
if f, err := fs.serveSourceTree(s.XContext(), requestName); err == nil {
757+
log.WithField(`request`, requestName).
758+
Print(`Found without prefix`)
690759
return f, nil
691760
}
692761

693762
if isIndex {
694763
// If there was no index.html file in any dirs, supply our own.
764+
log.WithField(`request`, requestName).
765+
Print(`Created faked index.html file`)
695766
return newFakeFile("index.html", []byte(`<html><head><meta charset="utf-8"><script src="`+base+`.js"></script></head><body></body></html>`)), nil
696767
}
697768

769+
log.WithField(`request`, requestName).
770+
Error(`Failed to find`)
698771
return nil, os.ErrNotExist
699772
}
700773

0 commit comments

Comments
 (0)