RangerMauve / webrun

Run Web-first ESM modules outside of the browser
https://webrun-presentation.hashbase.io/
MIT License
77 stars 8 forks source link

fetch does not work with file:// urls #14

Open Gozala opened 5 years ago

Gozala commented 5 years ago

I get following error when trying to read contents of the file that is located next to the module:

(node:16340) UnhandledPromiseRejectionWarning: TypeError: Only absolute URLs aresupported
    at getNodeRequestOptions (/Users/gozala/Projects/portal/node_modules/node-fetch/lib/index.js:1293:9)
    at /Users/gozala/Projects/portal/node_modules/node-fetch/lib/index.js:1393:19
    at new Promise (<anonymous>)
    at fetch (/Users/gozala/Projects/portal/node_modules/node-fetch/lib/index.js:1390:9)
    at main (file:///Users/gozala/Projects/portal/src/main.js?allow-require=true&allowRequire=true:9:21)
    at file:///Users/gozala/Projects/portal/src/main.js?allow-require=true&allowRequire=true:35:1
    at SourceTextModule.evaluate (internal/vm/source_text_module.js:228:25)
    at DefaultWebrun.import (/Users/gozala/Projects/portal/node_modules/@rangermauve/webrun/src/Webrun.js:37:20)
(node:16340) UnhandledPromiseRejectionWarning: Unhandled promise rejection. Thiserror originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:16340) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate theNode.js process with a non-zero exit code.

I believe that is due to the fact that fetch does not seem to support file:// urls

RangerMauve commented 5 years ago

Yeah, I don't have fetch supporting different resource types yet. Does it work with fetch in the browser?

Gozala commented 5 years ago

Yeah, I don't have fetch supporting different resource types yet. Does it work with fetch in the browser?

I think it does if your app is loaded from file:// url. I could be wrong though.

Gozala commented 5 years ago

Truth is I did not necessarily cared about file urls per say, mostly it was to read some file via module relative path. On a web usually just resolve url and fetch it. So maybe modules should not have file:// url but something like resource:/// or app:/// where root instead ? That would address this use case without exposing full read access to filesystem.

RangerMauve commented 5 years ago

I'd rather not stray too far from stuff that would work in the web. đŸ˜…

I just tried and fetch doesn't support file:// URLs. I think #15 with the file API would be a safer bet for now.

Gozala commented 5 years ago

I just tried and fetch doesn't support file:// URLs. I think #15 with the file API would be a safer bet for now.

That is why I suggested to maybe use different schema than "file://" because on the web you usually resolve URL relative to something and then fetch it.

Gozala commented 5 years ago
screen shot 2018-12-12 at 2 05 28 pm

And in fact fetch on file URLs do work as long as the files you're trying to fetch are under the same path as document doing the fetch.

RangerMauve commented 5 years ago

Weird, I tied doing fetch(window.location.href) in Chrome which resulted in it complaining about CORS not being set.

I was thinking of integrating the JSDOM resource loader and tying it together with the protocol loading stuff used for loading modules.

Gozala commented 5 years ago

hmm it could be that chrome deals with that differently, I did try on Firefox

Gozala commented 5 years ago

Either way. Primary point I was trying to make is that it's not uncommon to fetch stuff with a relative URL. The fact that it happens to be file:// URL in this instance isn't a secondary IMO.

RangerMauve commented 5 years ago

Yeah, I agree. My biggest worry is how to handle all the different semantics of fetch for every type of protocol. Like, the obvious fetch(url) case doesn't require much thinking, but there's various headers and partial downloads which might get difficult. I'm sure I could work off of whatever JSDOM did, though.