-
Notifications
You must be signed in to change notification settings - Fork 570
How to properly polyfill fetch/XMLHttpRequest in node #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I would recommend to see my Comment on #518. However it should be mentioned that polyfilling in this case meant including the polyfill in the build with a name .inc.js, not doing anything with the output. The polyfill must be present at transpile time, not at node.js runtime. |
@MartinSahlen I tried with an
Is that really true? The check appears to be done at runtime. Is there an additional check at compile time? (and how would that even be done, since the transpiler doesn't know which JavaScript engine/browser is being targeted?) |
@flimzy TBH, I will not write my claims in stone. They are just based on lengthy trial and error. I tried for instance, just stubbing out However including it in the build step made it "work", as in it: The code tried to do a request but got strange errors (even after properly polyfilling I would 1) recommend you read my comment here #518 (comment) to read a bit more about what seems to be needed to do a polyfill, at least from empirical observations and 2) check out https://github.com/MartinSahlen/go-cloud-fn for a working example The docs are regrettingly pretty scarce on this one so as I said these are just based on my experiments. Would be happy to be proven wrong and devised a simpler solution 😅 |
Thanks for the additional information, and the link to go-cloud-fn. However, I don't see a
Am I missing something? |
I'm sorry, I'm just assuming we are all in the same state of mind here. So, what I found is that doing a In my case (https://github.com/MartinSahlen/go-cloud-fn/blob/master/node-compat/webpack.config.js) I set the output to If you look at the https://github.com/MartinSahlen/go-cloud-fn/blob/master/package.json file you can see specifically how this is done. Does this make sense to you @flimzy ? |
Ahh, I did miss that your |
Today I'm finally getting back to this issue, and today I have no problems at all. I'm not sure what I was doing differently before, but today everything works exactly as expected when I simply do the same as you with:
|
I think I was previously trying various |
What fails? Have you remembered to wrap http calls in goroutines to avoid blocking? |
And are you using webpack to bundle dependencies? Just curious to know if your setup deviates from mine, as I have this confirmed working. |
With
With
(with no stack trace). I'm attempting to debug the When using the various |
No, I'm not using webpack. I just added the following code to an
|
I have expanded on my investigation of the problem with |
When I was looking over http://redux.js.org/docs/advanced/AsyncActions.html#async-action-creators, I spotted a mention of a polyfill for Fetch API for node:
Given that GopherJS implements an http.RoundTripper based on Fetch API in its implementation of I don't see it mentioned here, but in the previous related issue #518, @MartinSahlen said he tried it:
@MartinSahlen, what was your experience with |
I suppose I should try to find my polyfill for the It implements |
On the topic of incorporating some node module for this (/cc @flimzy), I wonder if we can reuse the approach used for installing the syscall module? E.g., https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md. |
My experience with |
I used the |
Uh oh!
There was an error while loading. Please reload this page.
The compatibility table mentions partial support for 'net/http', saying:
But I'm not having any luck figuring out how to get such a polyfill to work. The API detection happens very early in the Go program initialization, such that, as far as I can tell, it is impossible to load a polyfill in time for it to be detected.
Is the only option to prepend GopherJS's output with something like
var fetch = require('fetch-node');
before executing it?The text was updated successfully, but these errors were encountered: