Skip to content

Commit

Permalink
feat(rpc): enable custom http agent and https agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Aug 27, 2019
1 parent 04a242d commit 34fca52
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,28 @@ After that you can use the `core` object to generate addresses, send requests, e

# RPC

## Default RPC

Please see [Default RPC](https://github.com/nervosnetwork/ckb-sdk-js/blob/develop/packages/ckb-sdk-rpc/src/defaultRPC.ts#L165)

## Persistent Connection

Please add `httpAgent` or `httpsAgent` to enable the persistent connection.

If the SDK is running in Node.js, the following steps make the persistent connection available.

```javascript
// HTTP Agent
const http = require('http')
const httpAgent = new http.Agent({ keepAlive: true })
core.rpc.setNode({ httpAgent })

// HTTPS Agent
const https = require('https')
const httpsAgent = new https.Agent({ keepAlive: true })
core.rpc.setNode({ httpsAgent })
```

# Errors

1. RPC Errors
Expand Down
22 changes: 21 additions & 1 deletion packages/ckb-sdk-rpc/__tests__/ckb-rpc-helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const path = require('path')
const http = require('http')
const https = require('https')

const env = path.join(__dirname, '../.env')

Expand Down Expand Up @@ -32,14 +34,32 @@ describe('ckb-rpc settings and helpers', () => {
rpc.setDebugLevel(DebugLevel.Off)
})

it('set node', () => {
it('set node url', () => {
const node = {
url: 'http://localhost:8114',
}
rpc.setNode(node)
expect(rpc.node).toEqual(node)
})

it('set http agent', () => {
const httpAgent = new http.Agent()
const node = {
httpAgent,
}
rpc.setNode(node)
expect(rpc.node.httpAgent).toBeDefined()
})

it('set https agent', () => {
const httpsAgent = new https.Agent()
const node = {
httpsAgent,
}
rpc.setNode(node)
expect(rpc.node.httpsAgent).toBeDefined()
})

it('has 27 default rpc', () => {
expect(rpc.methods.length).toBe(27)
})
Expand Down
2 changes: 2 additions & 0 deletions packages/ckb-sdk-rpc/src/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Method {
},
data: payload,
url: this.node.url,
httpAgent: this.node.httpAgent,
httpsAgent: this.node.httpsAgent,
}).then(res => {
if (res.data.id !== id) {
throw new Error('JSONRPC id not match')
Expand Down
2 changes: 2 additions & 0 deletions packages/ckb-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ declare namespace CKBComponents {
export type Since = string
export interface Node {
url: string
httpAgent?: any
httpsAgent?: any
}
export interface Method {
name: string
Expand Down

0 comments on commit 34fca52

Please sign in to comment.