1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

Better handle remote urls in ResolveModule

This commit is contained in:
Ryan Dahl 2018-05-28 09:48:33 -04:00
parent 6097f87154
commit 6a489daa02
3 changed files with 73 additions and 9 deletions

View file

@ -34,8 +34,9 @@ func CacheFileName(filename string, sourceCodeBuf []byte) string {
// Fetches a remoteUrl but also caches it to the localFilename.
func FetchRemoteSource(remoteUrl string, localFilename string) ([]byte, error) {
//println("FetchRemoteSource", remoteUrl)
assert(strings.HasPrefix(localFilename, SrcDir), localFilename)
logDebug("FetchRemoteSource %s %s", remoteUrl, localFilename)
assert(strings.HasPrefix(localFilename, SrcDir),
"Expected filename to start with SrcDir: "+localFilename)
var sourceReader io.Reader
file, err := os.Open(localFilename)

34
os.go
View file

@ -41,10 +41,36 @@ func InitOS() {
})
}
func SrcFileToUrl(filename string) string {
assert(len(SrcDir) > 0, "SrcDir shouldn't be empty")
if strings.HasPrefix(filename, SrcDir) {
rest := strings.TrimPrefix(filename, SrcDir)
if rest[0] == '/' {
rest = rest[1:]
}
return "http://" + rest
} else {
return filename
}
}
func ResolveModule(moduleSpecifier string, containingFile string) (
moduleName string, filename string, err error) {
logDebug("os.go ResolveModule moduleSpecifier %s containingFile %s", moduleSpecifier, containingFile)
logDebug("os.go ResolveModule moduleSpecifier %s containingFile %s",
moduleSpecifier, containingFile)
containingFile = SrcFileToUrl(containingFile)
moduleSpecifier = SrcFileToUrl(moduleSpecifier)
// Hack: If there is no extension, just add .ts
if path.Ext(moduleSpecifier) == "" {
moduleSpecifier = moduleSpecifier + ".ts"
}
logDebug("os.go ResolveModule after moduleSpecifier %s containingFile %s",
moduleSpecifier, containingFile)
moduleUrl, err := url.Parse(moduleSpecifier)
if err != nil {
@ -56,7 +82,7 @@ func ResolveModule(moduleSpecifier string, containingFile string) (
}
resolved := baseUrl.ResolveReference(moduleUrl)
moduleName = resolved.String()
if moduleUrl.IsAbs() {
if resolved.IsAbs() {
filename = path.Join(SrcDir, resolved.Host, resolved.Path)
} else {
filename = resolved.Path
@ -83,8 +109,8 @@ func HandleCodeFetch(moduleSpecifier string, containingFile string) (out []byte)
return
}
logDebug("CodeFetch moduleSpecifier %s containingFile %s filename %s",
moduleSpecifier, containingFile, filename)
logDebug("CodeFetch moduleName %s moduleSpecifier %s containingFile %s filename %s",
moduleName, moduleSpecifier, containingFile, filename)
if isRemote(moduleName) {
sourceCodeBuf, err = FetchRemoteSource(moduleName, filename)

View file

@ -11,7 +11,8 @@ func AssertEqual(t *testing.T, actual string, expected string) {
}
}
func TestResolveModule(t *testing.T) {
func TestResolveModule1(t *testing.T) {
createDirs()
moduleName, filename, err := ResolveModule(
"http://localhost:4545/testdata/subdir/print_hello.ts",
"/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts")
@ -22,8 +23,11 @@ func TestResolveModule(t *testing.T) {
"http://localhost:4545/testdata/subdir/print_hello.ts")
AssertEqual(t, filename,
path.Join(SrcDir, "localhost:4545/testdata/subdir/print_hello.ts"))
}
moduleName, filename, err = ResolveModule(
func TestResolveModule2(t *testing.T) {
createDirs()
moduleName, filename, err := ResolveModule(
"./subdir/print_hello.ts",
"/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts")
if err != nil {
@ -33,10 +37,13 @@ func TestResolveModule(t *testing.T) {
"/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts")
AssertEqual(t, filename,
"/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts")
}
func TestResolveModule3(t *testing.T) {
createDirs()
// In the case where the containingFile is a directory (indicated with a
// trailing slash)
moduleName, filename, err = ResolveModule(
moduleName, filename, err := ResolveModule(
"testdata/001_hello.js",
"/Users/rld/go/src/github.com/ry/deno/")
if err != nil {
@ -47,3 +54,33 @@ func TestResolveModule(t *testing.T) {
AssertEqual(t, filename,
"/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js")
}
func TestResolveModule4(t *testing.T) {
createDirs()
// Files in SrcDir should resolve to URLs.
moduleName, filename, err := ResolveModule(
path.Join(SrcDir, "unpkg.com/liltest@0.0.5/index.ts"),
".")
if err != nil {
t.Fatalf(err.Error())
}
AssertEqual(t, moduleName,
"http://unpkg.com/liltest@0.0.5/index.ts")
AssertEqual(t, filename,
"/Users/rld/.deno/src/unpkg.com/liltest@0.0.5/index.ts")
}
func TestResolveModule5(t *testing.T) {
createDirs()
// Files in SrcDir should resolve to URLs.
moduleName, filename, err := ResolveModule(
"./util",
path.Join(SrcDir, "unpkg.com/liltest@0.0.5/index.ts"))
if err != nil {
t.Fatalf(err.Error())
}
AssertEqual(t, moduleName,
"http://unpkg.com/liltest@0.0.5/util.ts")
AssertEqual(t, filename,
path.Join(SrcDir, "unpkg.com/liltest@0.0.5/util.ts"))
}