summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-05-28 09:48:33 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-05-28 11:20:32 -0400
commit6a489daa0206abecdae240d5e2ac4739a5682896 (patch)
tree641d72971404b76797e84192992e71775b6a49a9
parent6097f871542dcd8486ef1a7c508f0f373297c74a (diff)
Better handle remote urls in ResolveModule
-rw-r--r--deno_dir.go5
-rw-r--r--os.go34
-rw-r--r--os_test.go43
3 files changed, 73 insertions, 9 deletions
diff --git a/deno_dir.go b/deno_dir.go
index 60378f689..84ead16f7 100644
--- a/deno_dir.go
+++ b/deno_dir.go
@@ -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)
diff --git a/os.go b/os.go
index 479cefb83..2fbbd95a5 100644
--- a/os.go
+++ b/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) (
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)
diff --git a/os_test.go b/os_test.go
index ac94b5a3c..7fefcad8c 100644
--- a/os_test.go
+++ b/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(
"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"))
+}