mirror of
https://github.com/denoland/deno.git
synced 2025-02-12 16:59:32 -05:00
Better handle remote urls in ResolveModule
This commit is contained in:
parent
6097f87154
commit
6a489daa02
3 changed files with 73 additions and 9 deletions
|
@ -34,8 +34,9 @@ func CacheFileName(filename string, sourceCodeBuf []byte) string {
|
||||||
|
|
||||||
// Fetches a remoteUrl but also caches it to the localFilename.
|
// Fetches a remoteUrl but also caches it to the localFilename.
|
||||||
func FetchRemoteSource(remoteUrl string, localFilename string) ([]byte, error) {
|
func FetchRemoteSource(remoteUrl string, localFilename string) ([]byte, error) {
|
||||||
//println("FetchRemoteSource", remoteUrl)
|
logDebug("FetchRemoteSource %s %s", remoteUrl, localFilename)
|
||||||
assert(strings.HasPrefix(localFilename, SrcDir), localFilename)
|
assert(strings.HasPrefix(localFilename, SrcDir),
|
||||||
|
"Expected filename to start with SrcDir: "+localFilename)
|
||||||
var sourceReader io.Reader
|
var sourceReader io.Reader
|
||||||
|
|
||||||
file, err := os.Open(localFilename)
|
file, err := os.Open(localFilename)
|
||||||
|
|
34
os.go
34
os.go
|
@ -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) (
|
func ResolveModule(moduleSpecifier string, containingFile string) (
|
||||||
moduleName string, filename string, err error) {
|
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)
|
moduleUrl, err := url.Parse(moduleSpecifier)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -56,7 +82,7 @@ func ResolveModule(moduleSpecifier string, containingFile string) (
|
||||||
}
|
}
|
||||||
resolved := baseUrl.ResolveReference(moduleUrl)
|
resolved := baseUrl.ResolveReference(moduleUrl)
|
||||||
moduleName = resolved.String()
|
moduleName = resolved.String()
|
||||||
if moduleUrl.IsAbs() {
|
if resolved.IsAbs() {
|
||||||
filename = path.Join(SrcDir, resolved.Host, resolved.Path)
|
filename = path.Join(SrcDir, resolved.Host, resolved.Path)
|
||||||
} else {
|
} else {
|
||||||
filename = resolved.Path
|
filename = resolved.Path
|
||||||
|
@ -83,8 +109,8 @@ func HandleCodeFetch(moduleSpecifier string, containingFile string) (out []byte)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logDebug("CodeFetch moduleSpecifier %s containingFile %s filename %s",
|
logDebug("CodeFetch moduleName %s moduleSpecifier %s containingFile %s filename %s",
|
||||||
moduleSpecifier, containingFile, filename)
|
moduleName, moduleSpecifier, containingFile, filename)
|
||||||
|
|
||||||
if isRemote(moduleName) {
|
if isRemote(moduleName) {
|
||||||
sourceCodeBuf, err = FetchRemoteSource(moduleName, filename)
|
sourceCodeBuf, err = FetchRemoteSource(moduleName, filename)
|
||||||
|
|
43
os_test.go
43
os_test.go
|
@ -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(
|
moduleName, filename, err := ResolveModule(
|
||||||
"http://localhost:4545/testdata/subdir/print_hello.ts",
|
"http://localhost:4545/testdata/subdir/print_hello.ts",
|
||||||
"/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.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")
|
"http://localhost:4545/testdata/subdir/print_hello.ts")
|
||||||
AssertEqual(t, filename,
|
AssertEqual(t, filename,
|
||||||
path.Join(SrcDir, "localhost:4545/testdata/subdir/print_hello.ts"))
|
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",
|
"./subdir/print_hello.ts",
|
||||||
"/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts")
|
"/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts")
|
||||||
if err != nil {
|
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")
|
"/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts")
|
||||||
AssertEqual(t, filename,
|
AssertEqual(t, filename,
|
||||||
"/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts")
|
"/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
|
// In the case where the containingFile is a directory (indicated with a
|
||||||
// trailing slash)
|
// trailing slash)
|
||||||
moduleName, filename, err = ResolveModule(
|
moduleName, filename, err := ResolveModule(
|
||||||
"testdata/001_hello.js",
|
"testdata/001_hello.js",
|
||||||
"/Users/rld/go/src/github.com/ry/deno/")
|
"/Users/rld/go/src/github.com/ry/deno/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -47,3 +54,33 @@ func TestResolveModule(t *testing.T) {
|
||||||
AssertEqual(t, filename,
|
AssertEqual(t, filename,
|
||||||
"/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js")
|
"/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"))
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue