oracle / graaljs

GraalJS – A high-performance, ECMAScript compliant, and embeddable JavaScript runtime for Java
https://www.graalvm.org/javascript/
Universal Permissive License v1.0
1.81k stars 190 forks source link

URI based modules should be loaded using the FileSystem parsePath(URI) #257

Closed pmlopes closed 4 years ago

pmlopes commented 4 years ago

consider the following script:

import ms from 'https://unpkg.com/@esm/ms';
// not really important after this line
// it's just to ensure that no code elimination
// hapens
console.log(ms('2 days')); 

When using a custom FileSystem the module string path will be passed to the API method:

public Path parsePath(String path) {
  // path will be: "https://unpkg.com/@esm/ms"
  ...
}

Instead of:

public Path parsePath(URI uri) {
  // never called
  ...
}

As URIs are valid for module locations the correct FileSystem method should be invoked. Also the API contract states that parsePath(String) should be invoked for resources with schema file which isn't the case in this example.

eleinadani commented 4 years ago

Thanks for the report! Should be fixed by https://github.com/graalvm/graaljs/commit/b978c76b59cc9fc90e30cd6599c21f6e1ab35b5e

jazdw commented 4 years ago

@pmlopes Did you have to implement a custom file system that supports https to get this to work?

pmlopes commented 4 years ago

Hi, not yet, it's on my to-do list. I do have a custom filesystem but this method wasn't working. So I still need to implement it now

jazdw commented 4 years ago

OK, no worries. There's some discussion in the issue linked above.

jazdw commented 4 years ago

@eleinadani This still doesn't seem to work in 20.2 despite your fix. com.oracle.truffle.js.runtime.objects.DefaultESModuleLoader#resolveImportedModule refuses to try and parse the path as a URI and instead calls resolveSibling().

This is with a custom FS and URI scheme.

Error in script "": java.nio.file.InvalidPathException: Illegal char <:> at index 9: filestore://public/nashorn_import.js
org.graalvm.polyglot.PolyglotException: java.nio.file.InvalidPathException: Illegal char <:> at index 9: filestore://public/nashorn_import.js
    at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
    at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
    at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
    at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
    at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:229)
    at java.base/java.nio.file.Path.resolveSibling(Path.java:574)
    at com.oracle.truffle.api.TruffleFile.resolveSibling(TruffleFile.java:595)
    at com.oracle.truffle.js.runtime.objects.DefaultESModuleLoader.resolveImportedModule(DefaultESModuleLoader.java:98)
jazdw commented 4 years ago

I think your tests work because the source is not a file?