trezor-suite icon indicating copy to clipboard operation
trezor-suite copied to clipboard

Remove cross-fetch lib as we use Node18 everywhere

Open tomasklim opened this issue 1 year ago • 7 comments

  • remove cross-fetch
  • BEWARE of Timeout Issue. SEE: https://github.com/trezor/trezor-suite/pull/11820
  • beware of https://github.com/trezor/trezor-suite/pull/10137

tomasklim avatar Dec 01 '23 10:12 tomasklim

Removed from several packages in https://github.com/trezor/trezor-suite/pull/11223.

Removing in from @trezor/coinjoin would break coordinatorRequest test because request fails - unlike if cross-fetch is used.

Removing it from @trezor/connect requires reworking the mocks.

komret avatar Feb 21 '24 15:02 komret

Lets not do this before Node19. It has some differeneces and causing troubles. See: https://github.com/trezor/trezor-suite/pull/11820

(Details: https://satoshilabs.slack.com/archives/C02V2PSDNA2/p1711539811017599)

peter-sanderson avatar May 03 '24 10:05 peter-sanderson

Here is also relevant Github Issue: https://github.com/nodejs/node/issues/46375

peter-sanderson avatar May 03 '24 11:05 peter-sanderson

Also good read: https://betterstack.com/community/guides/scaling-nodejs/nodejs-timeouts/#timing-out-a-fetch-api-request

It says that in node fetch has no timeout, but it seems that its not true

In browsers, fetch() usually times out after a set period of time which varies amongst browsers. For example, in Firefox this timeout is set to 90 seconds by default, but in Chromium, it is 300 seconds. In Node.js, no default timeout is set for fetch() requests, but the newly added AbortSignal.timeout() API provides an easy way to cancel a fetch() request when a timeout is reached.

peter-sanderson avatar May 03 '24 11:05 peter-sanderson

Finally another issue: https://github.com/nodejs/undici/issues/1373

TBH I dont see a good solution to this right now :(

peter-sanderson avatar May 03 '24 12:05 peter-sanderson

Maybe a solution is to use undici. There is a way how to define timeouts globally.

const { Agent } = require('undici');


dispatcher = new Agent({
    connectTimeout: 10 * 60e3, // 10 minutes
    bodyTimeout: 10 * 60e3, // 10 minutes
    headersTimeout: 10 * 60e3, // 10 minutes
})

globalThis[Symbol.for('undici.globalDispatcher.1')] = dispatcher


// ----------------------------------------------------------------------------

const t = Date.now()

const run = async () => {
    try {
        await fetch("https://192.168.0.1");
    } finally {
        console.log((Date.now() - t) / 1000);
    }
}

run();

Source of the idea: https://github.com/nodejs/undici/issues/1373#issuecomment-1426025119

peter-sanderson avatar May 03 '24 12:05 peter-sanderson

It's not pressing, let's update node first and see if it works.

komret avatar May 06 '24 12:05 komret