Skip to content
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

Migrate to ofetch #904

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open

Migrate to ofetch #904

wants to merge 33 commits into from

Conversation

wa0x6e
Copy link
Contributor

@wa0x6e wa0x6e commented Sep 21, 2023

🧿 Current issues / What's wrong ?

Current fetch request does not support timeout, nor keep alive connections.

💊 Fixes / Solution

Migrate the fetch library from cross-fetch to ofetch.

The error handling changes from ofetch will also introduce a change in returned response on error.

Previously, error handling looks like:

try {
  const body = await getScores(x);
  if (body.error) { // Handle error from score-api };
  return body.result;
} catch(e) {
  // do something with e, which is an Error object, for network error, or JSON parse error
}

With ofetch changes,

try {
  const body = await getScores(x);
  return body.result;
} catch(e) {
  // do something with e, which is an JSON-RPC object
  if (typeof e.code === 'string') {
    // Error from score-api
  } else {
    // Network error 
  }
}

🚧 Changes

  • Add support for keep alive connections
  • Add timeout to all fetch requests
  • All non-200 status code throw an error
  • All errors thrown follow the same format as the underlying service error, beside getJSON and ipfsGet (see below)

getScores, validate and getVp

  • SUCCESS: The promise will resolve with the score JSON object
  • ERROR (from score-api): Promise rejection with JSON-RPC object (code === string)
  • ERROR (network): Promise rejection with JSON-RPC object (code === number)

The last argument (options) accepts a timeout property.

subgrapherRequest and getSnapshot

  • SUCCESS: return the JSON object
  • ERROR (subgrapher): return a JSON graphQL error object
  • ERROR (network): return a GraphQL like error object
  • ERROR (not json response): return a GraphQL like error object

The last argument (options) accepts a timeout property.

getJSON and ipfsGet

  • SUCCESS: return the JSON object
  • ERROR (network): throw a FetchError
  • ERROR (not json response): throw an JSON parse error

The last argument (options) accepts a timeout property.

send (from Client)

  • SUCCESS: return the JSON object
  • ERROR (from sequencer): Promise rejection with JSON-RPC object
  • ERROR (network): throw FetchError

‼️ Breaking changes

  • ofetch is handling error response differently, and is rejecting a Promise on all non-200 errors.

This new update will require a bump to the next major version (1.x.x).
As this new breaking change need more testing before public release, the new 1.x.x should be released as a beta, and used only by our internal services first (and keep updated with the main branch) for a few weeks.

Once everything confirmed as working as expected, then we can public release it.

🛠️ Tests

  • Call the getScores, validate, getSnapshot and getVp functions with valid data
  • It should return the expected object
  • Call them again with invalid data
  • It should return a JSON-RPC error object, from score-api
  • Call them again with invalid url (pass something else as score-api url)
  • It should return JSON-RPC error object

@wa0x6e wa0x6e marked this pull request as ready for review September 21, 2023 14:51
@codecov
Copy link

codecov bot commented Sep 21, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (350a1ef) 44.19% compared to head (bec2e3a) 72.15%.

❗ Current head bec2e3a differs from pull request most recent head cfb0e3e. Consider uploading reports for the commit cfb0e3e to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##           master     #904       +/-   ##
===========================================
+ Coverage   44.19%   72.15%   +27.96%     
===========================================
  Files          22       22               
  Lines        2100     2144       +44     
  Branches      195      249       +54     
===========================================
+ Hits          928     1547      +619     
+ Misses       1162      590      -572     
+ Partials       10        7        -3     
Files Coverage Δ
src/sign/index.ts 54.66% <100.00%> (+54.66%) ⬆️
src/utils.ts 61.60% <99.14%> (+26.06%) ⬆️

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wa0x6e wa0x6e changed the title Migrate ofetch Migrate to ofetch Sep 21, 2023
src/utils.ts Outdated Show resolved Hide resolved
src/utils.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@Todmy Todmy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

Copy link
Contributor

@Todmy Todmy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

@wa0x6e wa0x6e requested a review from Todmy September 22, 2023 16:00
@wa0x6e
Copy link
Contributor Author

wa0x6e commented Sep 22, 2023

Fixed the fetch request from Client, please review again, as it's a core function

package.json Show resolved Hide resolved
src/sign/index.ts Show resolved Hide resolved
@wa0x6e wa0x6e requested a review from Todmy September 25, 2023 15:00
@wa0x6e wa0x6e mentioned this pull request Sep 25, 2023
Copy link
Contributor

@Todmy Todmy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

src/sign/index.ts Show resolved Hide resolved
package.json Outdated
@@ -1,6 +1,6 @@
{
"name": "@snapshot-labs/snapshot.js",
"version": "0.6.2",
"version": "1.0.0-beta.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? We shouldn't go from a normal version to a beta. Also not sure we should bump to v1. I dont see any change in function or parameters

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty major change in error handling from ofetch, as pointed out in yesterday call. Beta will be released and tested only interbally first, and not for public

@bonustrack
Copy link
Member

@Todmy Can you test this PR, utACK is not enough

@Todmy Todmy self-requested a review September 26, 2023 10:25
@Todmy
Copy link
Contributor

Todmy commented Sep 29, 2023

@wa0x6e could you please test your tests? I run them and have some failed
Screenshot 2023-09-29 at 11 44 32
a lot of them related to the https://httpstat.us/200?sleep=5000 but there are other errors

Copy link
Contributor

@Todmy Todmy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests should be fixed

@wa0x6e
Copy link
Contributor Author

wa0x6e commented Sep 29, 2023

@wa0x6e could you please test your tests? I run them and have some failed Screenshot 2023-09-29 at 11 44 32 a lot of them related to the https://httpstat.us/200?sleep=5000 but there are other errors

Tests are passing on my local, and on github actions 🤔

What are the errors you're seeing ?

@wa0x6e
Copy link
Contributor Author

wa0x6e commented Sep 29, 2023

Added retry to flaky tests depending on third party services

@wa0x6e wa0x6e requested a review from Todmy September 29, 2023 18:07
@Todmy
Copy link
Contributor

Todmy commented Oct 2, 2023

issue persists. The reason why tests fail on my local host is that I use node@18. This package is used for projects with node@18 as well.

@wa0x6e
Copy link
Contributor Author

wa0x6e commented Oct 2, 2023

Hmmmm… have to start adding node 18 to ci

@wa0x6e
Copy link
Contributor Author

wa0x6e commented Oct 2, 2023

Hmmmm… have to start adding node 18 to ci

#911

@wa0x6e
Copy link
Contributor Author

wa0x6e commented Oct 3, 2023

issue persists. The reason why tests fail on my local host is that I use node@18. This package is used for projects with node@18 as well.

Tests fixed for node 18, also confirmed to work on node 20

Copy link
Contributor

@Todmy Todmy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK

@wa0x6e
Copy link
Contributor Author

wa0x6e commented Oct 5, 2023

I believe this should not be merged on master, but on a dedicated v1 branch. We could then npm publish the beta from that branch

@ChaituVR
Copy link
Member

ChaituVR commented Oct 6, 2023

Maybe we can leave it on this branch, can i publish it to beta tag?

@wa0x6e
Copy link
Contributor Author

wa0x6e commented Oct 6, 2023

Maybe we can leave it on this branch, can i publish it to beta tag?

I don’t think it’s possible to have 2 versions on the same branch, it’ll create a merge conflict

@ChaituVR
Copy link
Member

ChaituVR commented Oct 9, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants