frictionlessdata / datapackage-js

A JavaScript library for working with Data Package.
http://frictionlessdata.io/
MIT License
43 stars 15 forks source link

isomorphic-fetch relative protocol error #57

Closed dumyan closed 7 years ago

dumyan commented 7 years ago

Using relative protocol URLs (//) in node works, but isomorphic-fetch throws the following error in the browser:

SyntaxError
      at XMLHttpRequest.open (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:458:15)
      at about:blank:49369:12
      at self.fetch (about:blank:49346:13)
      at _readURL (about:blank:25508:17)
      at Function.readFileOrURL (about:blank:25534:19)
      at Profiles._loadFile (about:blank:25400:33)
      at Profiles._loadRegistry (about:blank:25286:20)
      at about:blank:25206:14
      at new Profiles (about:blank:25205:13)
      at _callee$ (test/browser/profiles.js:18:30)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:64:40)
      at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:355:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:116:21)
      at step (test/browser/profiles.js:19:191)
      at test/browser/profiles.js:19:437
      at Context.<anonymous> (test/browser/profiles.js:19:99)

One alternative is to use qubyte/fetch-ponyfill and another is to use github/whatwg-fetch for the browser and bitinn/node-fetch for node.

roll commented 7 years ago

I think we just can't use // in general purpose library for registy url and schemas urls because the library could be used from file://.. source breaking all our links. So it will be just incorrect way to write urls. (cc @pwalsh @rufuspollock)

dumyan commented 7 years ago

@roll Since this is an issue only in the browser, one way we can approach it is to check if the user is running in browser and look up window.location.protocol to see if we should prepend http or https to the path. If the user is running in node we can force https. This way relative protocol (//) can be also used in the descriptors or for initializing a datapackage. We need to make it clear that paths starting with // will be interpreted as http or https though.

roll commented 7 years ago

NOT ACTUAL for now we don't use relative protocol inside the codebase