Js-ipfs: Doesn't work inside a service-worker on Firefox

Created on 1 Sep 2018  ยท  21Comments  ยท  Source: ipfs/js-ipfs

By playing around with js-ipfs-api inside a service worker, I have noticed that it works perfectly on Safari and Chrome, but apparently not on Firefox.

The error reported is related to the usage of XMLHTTPRequest.

I guess this is the cause: https://www.fxsitecompat.com/en-CA/docs/2015/xmlhttprequest-is-no-longer-available-in-service-workers/

Any guess how to solve/fix this to support also Firefox service workers?

diexpert help wanted kinbug http-client

Most helpful comment

@alanshaw this is related to stream-http going xhr mode instead fetch mode when it doesn't detect web stream support, and sw doesn't have xhr. I'm gonna reopen and assign it to me.

All 21 comments

That is correct, XMLHTTPRequest is not supported anymore on service workers.

Accordingly, XMLHTTPRequest needs to be changed and the fetch API seems like the best option.

We fixed a XMLHTTPRequest in js-ipfs js-ipfs#1478. Would you like to create a PR for solving this here?

I will try to find the time for it ๐Ÿ‘

Should I update it for all the requests? I was diving a little and I found this line:
https://github.com/ipfs/js-ipfs-api/blob/231c4d78723d41152e6a695a5f171feae2274c19/src/utils/send-request.js#L174

That connects to:
https://github.com/ipfs/js-ipfs-api/blob/231c4d78723d41152e6a695a5f171feae2274c19/src/utils/request.js#L1-L12
https://github.com/ipfs/js-ipfs-api/blob/master/src/utils/request.js

I see from the code that there is a lot of dependency on how we are using this module. I guess I need to change that... but my bet is that would complicate a lot of things since changing it would mean to change the way we set up headers, the requests etc etc etc.

In any case can you guide a little bit me to find what would be the best solution? Maybe also @alanshaw can give me some suggestion _a la planning_ ๐Ÿ˜ƒ

Nice research!

I believe that we should have fetch for every single request, since each api call should be accessible through a service worker as well.

Yes, I suppose it will not be an immediate and easy change, as you say, some of the request's building blocks have to be implemented according to the fetch API. Let's wait for @alanshaw input here.

In the browser we use stream-http package that is suppose to use fetch if it's available we should report this problem to them

HI @koalalorenzo, I think this is because ReadableStream() is disabled in Firefox by default.
This function should be available for stream-http to work as a fetch() mode.

Could you test your code after enabling ReadableStream() by procedure below?

https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream

From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true) and the javascript.options.streams preference (needs to be set to true). To change preferences in Firefox, visit about:config.

The error that I am getting when I am doing a simple list of pinned elements is reporting a XMLHTTPRequest so I am not sure how much that is related to ReadableStream, but I could be wrong.

screen shot 2018-09-10 at 09 06 47

https://www.fxsitecompat.com/en-CA/docs/2015/xmlhttprequest-is-no-longer-available-in-service-workers/

Enabling ReadableStream on Firefox it doesn't fix the problem. I think it is more related to the code that I was reporting, but again I am not 100% sure.

I'm looking into stream-http for other stuff I'll check this out.

@hugomrdias let me know if you need help with stream-http. I'm the maintainer of that module and also working for Protocol Labs.

Can anybody explain to me why it is stream-http the cause of this?

@koalalorenzo
It's because js-ipfs-api uses stream-http as an alternative of http in browser environment.
https://github.com/ipfs/js-ipfs-api/blob/master/package.json#L11

@koalalorenzo
Hi again, sorry for sending comments frequently. Thank you for testing my previous post. Sorry to hear that it didn't worked out for you. ๐Ÿ˜ข

I just tested ServiceWorker inside Firefox and was able to call ipfs.pin.ls() from it. My environment: Firefox (Quantum 61.0.2 on MacOS). Only change I did is enabling ReadableStreaming()

This is the screenshot of my console.
image
Sorry for random logs, I was in hurry...

From the result, I think if stream-http run in the fetch mode, ipfs.pin.ls() functions can be called from ServiceWorker. Could you set break point here and check what mode stream-http is running in your Firefox?

https://github.com/jhiesey/stream-http/blob/718f5127ec970e91ea4c323c2491efdffb22a1c9/lib/request.js#L10

Hope this helps. ๐Ÿ˜„

Well, I can't ask the users to enable ReadableStreaming :(
@jhiesey maybe you can help here

FYSA Streaming API is very close to be enabled by default in Firefox:

[..] the only blocker (or at least the main one I know of) is bug 1385890 and Jason's working on it. Hoping to ship soon.
โ€“ [meta] enable streams API by default

cc https://github.com/ipfs/in-web-browsers/issues/55

@koalalorenzo this should now be fixed in [email protected] by https://github.com/ipfs/js-ipfs-api/pull/868 please open a new issue if the problem persists

@alanshaw this is related to stream-http going xhr mode instead fetch mode when it doesn't detect web stream support, and sw doesn't have xhr. I'm gonna reopen and assign it to me.

argh, yes sorry, thanks @hugomrdias

Any update?

aaaand Firefox 65 just landed in stable channel with support for ReadableStream :tada:

2019-01-30--00-36-04

@koalalorenzo are you able to re-check with Firefox 65?

I am not able to test again as we decided to implement it in a different way in order to unlock what we were doing on Orion. Sorry :(

Was this page helpful?
0 / 5 - 0 ratings