-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rpc): make fetch customizable (#574)
- Loading branch information
Showing
7 changed files
with
87 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ckb-lumos/rpc": minor | ||
--- | ||
|
||
make `fetch` customizable |
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 @@ | ||
const { RPC } = require(".."); | ||
|
||
describe("custom fetch", () => { | ||
test("should work", async () => { | ||
const customizedFetch = jest.fn((_, { body }) => | ||
Promise.resolve({ | ||
json: () => | ||
Promise.resolve({ | ||
jsonrpc: "2.0", | ||
result: "0x8dd", | ||
id: JSON.parse(body).id, | ||
}), | ||
}) | ||
); | ||
|
||
const rpc = new RPC("", { fetch: customizedFetch }); | ||
await rpc.getTipBlockNumber(); | ||
|
||
expect(customizedFetch).toBeCalled(); | ||
expect(JSON.parse(customizedFetch.mock.calls[0][1].body)).toHaveProperty( | ||
"method", | ||
"get_tip_block_number" | ||
); | ||
}); | ||
}); |
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
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,6 @@ | ||
/// <reference lib="dom" /> | ||
|
||
export type RPCConfig = { | ||
timeout: number; | ||
fetch: typeof fetch; | ||
}; |
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,28 @@ | ||
# Migrate to Lumos v0.21 | ||
|
||
### Deprecated the `httpAgent` and `httpsAgent` in `RPC.setNode` | ||
|
||
Please use the `fetch` option in `RPC` constructor instead. | ||
|
||
```diff | ||
-const rpc = new RPC(url) | ||
+const rpc = new RPC( | ||
+ url, | ||
+ { fetch: (request, init) => originalFetch(request, { ...init, keepalive: true }) }, | ||
+) | ||
-rpc.setNode({ url, httpAgent, httpsAgent }) | ||
+rpc.setNode({ url }) | ||
``` | ||
|
||
If you are still in working with Node.js(or Electron) runtime, you can migrate to `node-fetch` to continue using the customized agent | ||
|
||
```ts | ||
import fetch from "node-fetch" | ||
import { Agent } from "http" | ||
|
||
const rpc = new RPC(url, { | ||
fetch: (request, init) => { | ||
return fetch(request, { ...init, httpAgent: new Agent({ keepAlive: true }) }) | ||
}, | ||
}) | ||
``` |
ce8eead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 New canary release:
0.0.0-canary-ce8eead-20231127085615
ce8eead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
lumos-website – ./
lumos-website.vercel.app
lumos-website-git-develop-magickbase.vercel.app
lumos-website-magickbase.vercel.app