-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create r2d2 fetch for web3 provider (#6443)
* feat: add r2d2 fetch function in background * feat: serialize response object for r2d2 fetch * refactor: review feedback * feat: add request serializer * feat: some api provider use r2d2 fetch * fix: cspell
- Loading branch information
Showing
20 changed files
with
164 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,6 +171,7 @@ | |
"realise", | ||
"rebalance", | ||
"redpacket", | ||
"regedit", | ||
"repayer", | ||
"replacestate", | ||
"repost", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export const r2d2URL = 'r2d2.to' | ||
|
||
export enum R2d2Workers { | ||
opensea = 'opensea-proxy', | ||
gitcoin = 'gitcoin-agent', | ||
coinMarketCap = 'coinmarketcap-agent', | ||
goPlusLabs = 'gopluslabs', | ||
} | ||
|
||
type R2d2WorkerMatchTuple = [string, R2d2Workers] | ||
|
||
const matchers: R2d2WorkerMatchTuple[] = [ | ||
['https://api.opensea.io', R2d2Workers.opensea], | ||
['https://gitcoin.co', R2d2Workers.gitcoin], | ||
['https://web-api.coinmarketcap.com', R2d2Workers.coinMarketCap], | ||
['https://api.gopluslabs.io', R2d2Workers.goPlusLabs], | ||
] | ||
|
||
/** | ||
* Why use r2d2 fetch: some third api provider will be block in Firefox and protect api key | ||
* @returns fetch response | ||
* @param input | ||
* @param init | ||
*/ | ||
export async function r2d2Fetch(input: RequestInfo, init?: RequestInit): Promise<Response> { | ||
const url = init instanceof Request ? init.url : (input as string) | ||
if (url.includes('r2d2.to')) return globalThis.fetch(input, init) | ||
|
||
const r2deWorkerType = matchers.find((x) => url.startsWith(x[0]))?.[1] | ||
|
||
if (!r2deWorkerType) { | ||
return globalThis.fetch(input, init) | ||
} | ||
|
||
const origin = new URL(url).origin | ||
const r2d2ProxyURL = url.replace(origin, `https://${r2deWorkerType}.${r2d2URL}`) | ||
|
||
return globalThis.fetch(r2d2ProxyURL, init) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { useAsyncRetry } from 'react-use' | ||
import { PluginGitcoinRPC } from '../messages' | ||
import { fetchGrant } from '../apis' | ||
|
||
export function useGrant(id: string) { | ||
return useAsyncRetry(() => PluginGitcoinRPC.fetchGrant(id)) | ||
return useAsyncRetry(() => fetchGrant(id)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
export { PLUGIN_ID, HD_PATH_WITHOUT_INDEX_ETHEREUM, UPDATE_CHAIN_STATE_DELAY } from '@masknet/plugin-wallet' | ||
|
||
export const OPENSEA_API_KEY = 'c38fe2446ee34f919436c32db480a2e3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/// <reference path="./env.d.ts" /> | ||
/// <reference path="./intl.d.ts" /> | ||
/// <reference path="./global.d.ts" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare function r2d2Fetch(url: RequestInfo, init?: RequestInit): Promise<Response> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { TypesonPromise } from 'typeson' | ||
|
||
export const is = (x: any) => x instanceof ReadableStream | ||
|
||
export const serializer = (x: ReadableStream) => { | ||
return new TypesonPromise<Uint8Array[]>(async (resolve, reject) => { | ||
const reader = x.getReader() | ||
const output = [] | ||
let isDone = false | ||
if (reader) { | ||
try { | ||
while (!isDone) { | ||
const { done, value } = await reader.read() | ||
if (!done) { | ||
output.push(value) | ||
} else { | ||
isDone = true | ||
} | ||
} | ||
} catch (error) { | ||
reject(error) | ||
} | ||
} | ||
resolve(output) | ||
}) | ||
} | ||
|
||
export const deserializer = (x: Uint8Array[]) => { | ||
return new ReadableStream({ | ||
start(controller) { | ||
for (const binary of x) { | ||
controller.enqueue(binary) | ||
} | ||
controller.close() | ||
}, | ||
}) | ||
} | ||
|
||
export const readableStreamRegedit = [is, serializer, deserializer] as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export const is = (x: any) => x instanceof Request | ||
export const serializer = (x: Request) => { | ||
const { url, method, body, headers, mode, credentials, cache, redirect, referrer, integrity } = x | ||
return { | ||
input: url, | ||
init: { | ||
method, | ||
// body maybe is a Blob, a BufferSource, a FormData, a URLSearchParams, a string, or a ReadableStream object, should handle different object type later | ||
body, | ||
headers, | ||
mode, | ||
credentials, | ||
cache, | ||
redirect, | ||
referrer, | ||
integrity, | ||
}, | ||
} | ||
} | ||
|
||
export const deserializer = (x: { input: string; init: RequestInit }) => { | ||
return new Request(x.input, x.init) | ||
} | ||
|
||
export const requestRegedit = [is, serializer, deserializer] as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export const is = (x: any) => x instanceof Response | ||
export const serializer = (x: Response) => { | ||
return { | ||
body: x.body, | ||
init: { | ||
status: x.status, | ||
statusText: x.statusText, | ||
headers: x.headers, | ||
}, | ||
} | ||
} | ||
|
||
export const deserializer = (x: { body: Uint8Array[]; init: ResponseInit }) => { | ||
const body = new ReadableStream({ | ||
start(controller) { | ||
for (const binary of x.body) { | ||
controller.enqueue(binary) | ||
} | ||
controller.close() | ||
}, | ||
}) | ||
return new Response(body, x.init) | ||
} | ||
|
||
export const responseRegedit = [is, serializer, deserializer] as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export const GO_PLUS_LABS_ROOT_URL = 'https://gopluslabs.r2d2.to' | ||
export const GO_PLUS_LABS_ROOT_URL = 'https://api.gopluslabs.io' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export const OPENSEA_ACCOUNT_URL = 'https://opensea.io/accounts/:address' | ||
export const OPENSEA_API_KEY = '1cf95be40f1e45449c0b63ccb4b64cef' | ||
export const OPENSEA_API_URL = 'https://api.opensea.io' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters