Skip to content

Commit

Permalink
feat: timeout option for http and webSocket (#79)
Browse files Browse the repository at this point in the history
* feat: add timeout as config to transport

* chore: changeset

* feat: decode log `topics` + `data` & infer types for logs (#75)

* feat: decode event log topics & data

* docs: createContractEventFilter

* pr review

* feat: fall back to `getLogs` if filters are not supported (#76)

* feat: fall back to getLogs if filters are not supported

* Update src/actions/public/watchContractEvent.test.ts

Co-authored-by: awkweb <[email protected]>

* pr review

---------

Co-authored-by: awkweb <[email protected]>

* feat: decorate clients with actions (#77)

* feat: fall back to getLogs if filters are not supported

* Update src/actions/public/watchContractEvent.test.ts

Co-authored-by: awkweb <[email protected]>

* pr review

* feat: decode event log topics & data

* wip: public

* wip: add ens

* docs: public actions

* wip: wallet actions

* docs: wallet actions

* wip: test actions

* wip: add utils to main entrypoint

* tests: add tests

* playgrounds: update

* chore: changeset

* test

* refactor: wire up decorator generics

* fix conflicts

* format

---------

Co-authored-by: awkweb <[email protected]>

---------

Co-authored-by: awkweb <[email protected]>
  • Loading branch information
jxom and tmm authored Feb 20, 2023
1 parent d6a29f5 commit db9caa9
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-cows-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Added `timeout` as a config option to the `http` and `webSocket` Transports.
14 changes: 14 additions & 0 deletions site/docs/clients/transports/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,17 @@ const transport = http('https://eth-mainnet.g.alchemy.com/v2/...', {
retryDelay: 100, // [!code focus]
})
```

### timeout (optional)

- **Type:** `number`
- **Default:** `10_000`

The timeout for requests.

```ts
const transport = http('https://eth-mainnet.g.alchemy.com/v2/...', {
timeout: 60_000, // [!code focus]
})
```

15 changes: 14 additions & 1 deletion site/docs/clients/transports/websocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,17 @@ The base delay (in ms) between retries. By default, the Transport will use [expo
const transport = webSocket('wss://eth-mainnet.g.alchemy.com/v2/...', {
retryDelay: 100, // [!code focus]
})
```
```

### timeout (optional)

- **Type:** `number`
- **Default:** `10_000`

The timeout for async WebSocket requests.

```ts
const transport = webSocket('wss://eth-mainnet.g.alchemy.com/v2/...', {
timeout: 60_000, // [!code focus]
})
```
8 changes: 8 additions & 0 deletions src/clients/createClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ test('creates', () => {
"request": [MockFunction spy],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "mock",
},
"type": "base",
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('transports', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": 10000,
"type": "http",
"url": undefined,
},
Expand Down Expand Up @@ -134,6 +136,7 @@ describe('transports', () => {
"retryCount": 3,
"retryDelay": 150,
"subscribe": [Function],
"timeout": 10000,
"type": "webSocket",
},
"type": "base",
Expand All @@ -160,6 +163,7 @@ describe('transports', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "custom",
},
"type": "base",
Expand Down Expand Up @@ -197,6 +201,7 @@ describe('config', () => {
"request": [MockFunction spy],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "mock",
},
"type": "base",
Expand Down Expand Up @@ -232,6 +237,7 @@ describe('config', () => {
"request": [MockFunction spy],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "mock",
},
"type": "base",
Expand Down Expand Up @@ -267,6 +273,7 @@ describe('config', () => {
"request": [MockFunction spy],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "mock",
},
"type": "base",
Expand Down Expand Up @@ -302,6 +309,7 @@ describe('config', () => {
"request": [MockFunction spy],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "mock",
},
"type": "foo",
Expand Down
4 changes: 4 additions & 0 deletions src/clients/createPublicClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ test('creates', () => {
"request": [MockFunction spy],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "mock",
},
"type": "publicClient",
Expand Down Expand Up @@ -148,6 +149,7 @@ describe('transports', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": 10000,
"type": "http",
"url": undefined,
},
Expand Down Expand Up @@ -233,6 +235,7 @@ describe('transports', () => {
"retryCount": 3,
"retryDelay": 150,
"subscribe": [Function],
"timeout": 10000,
"type": "webSocket",
},
"type": "publicClient",
Expand Down Expand Up @@ -293,6 +296,7 @@ describe('transports', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "custom",
},
"type": "publicClient",
Expand Down
3 changes: 3 additions & 0 deletions src/clients/createTestClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ test('creates', () => {
"request": [MockFunction spy],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "mock",
},
"type": "testClient",
Expand Down Expand Up @@ -145,6 +146,7 @@ describe('transports', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": 10000,
"type": "http",
"url": undefined,
},
Expand Down Expand Up @@ -226,6 +228,7 @@ describe('transports', () => {
"retryCount": 3,
"retryDelay": 150,
"subscribe": [Function],
"timeout": 10000,
"type": "webSocket",
},
"type": "testClient",
Expand Down
4 changes: 4 additions & 0 deletions src/clients/createWalletClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ test('creates', () => {
"request": [MockFunction spy],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "mock",
},
"type": "walletClient",
Expand Down Expand Up @@ -84,6 +85,7 @@ describe('transports', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "custom",
},
"type": "walletClient",
Expand Down Expand Up @@ -121,6 +123,7 @@ describe('transports', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": 10000,
"type": "http",
"url": "https://mockapi.com/rpc",
},
Expand Down Expand Up @@ -162,6 +165,7 @@ describe('transports', () => {
"retryCount": 3,
"retryDelay": 150,
"subscribe": [Function],
"timeout": 10000,
"type": "webSocket",
},
"type": "walletClient",
Expand Down
2 changes: 2 additions & 0 deletions src/clients/transports/createTransport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ test('default', () => {
"request": [MockFunction spy],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "mock",
},
"request": [Function],
Expand Down Expand Up @@ -52,6 +53,7 @@ test('value', () => {
"request": [MockFunction spy],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "mock",
},
"request": [Function],
Expand Down
5 changes: 4 additions & 1 deletion src/clients/transports/createTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export type TransportConfig<
retryDelay?: number
/** The max number of times to retry. */
retryCount?: number
/** The timeout (in ms) for requests. */
timeout?: number
/** The type of the transport. */
type: TType
}
Expand Down Expand Up @@ -52,12 +54,13 @@ export function createTransport<
request,
retryCount = 3,
retryDelay = 150,
timeout,
type,
}: TransportConfig<TType>,
value?: TRpcAttributes,
): ReturnType<Transport<TType, TRpcAttributes>> {
return {
config: { key, name, request, retryCount, retryDelay, type },
config: { key, name, request, retryCount, retryDelay, timeout, type },
request: buildRequest(request, { retryCount, retryDelay }),
value,
}
Expand Down
4 changes: 4 additions & 0 deletions src/clients/transports/custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ test('default', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "custom",
},
"request": [Function],
Expand All @@ -50,6 +51,7 @@ describe('config', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "custom",
},
"request": [Function],
Expand All @@ -74,6 +76,7 @@ describe('config', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "custom",
},
"request": [Function],
Expand All @@ -100,6 +103,7 @@ describe('config', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "custom",
},
"request": [Function],
Expand Down
6 changes: 6 additions & 0 deletions src/clients/transports/fallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ test('default', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"type": "fallback",
},
"request": [Function],
Expand All @@ -35,6 +36,7 @@ test('default', () => {
"request": [Function],
"retryCount": 0,
"retryDelay": 150,
"timeout": 10000,
"type": "http",
},
"request": [Function],
Expand All @@ -49,6 +51,7 @@ test('default', () => {
"request": [Function],
"retryCount": 0,
"retryDelay": 150,
"timeout": 10000,
"type": "http",
},
"request": [Function],
Expand Down Expand Up @@ -328,6 +331,7 @@ describe('client', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": undefined,
"transports": [
{
"config": {
Expand All @@ -336,6 +340,7 @@ describe('client', () => {
"request": [Function],
"retryCount": 0,
"retryDelay": 150,
"timeout": 10000,
"type": "http",
},
"request": [Function],
Expand All @@ -350,6 +355,7 @@ describe('client', () => {
"request": [Function],
"retryCount": 0,
"retryDelay": 150,
"timeout": 10000,
"type": "http",
},
"request": [Function],
Expand Down
33 changes: 33 additions & 0 deletions src/clients/transports/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assertType, describe, expect, test } from 'vitest'

import { localhost } from '../../chains'
import { wait } from '../../utils/wait'
import { createHttpServer } from '../../_test'

import type { HttpTransport } from './http'
Expand All @@ -20,6 +21,7 @@ test('default', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": 10000,
"type": "http",
},
"request": [Function],
Expand All @@ -44,6 +46,7 @@ describe('config', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": 10000,
"type": "http",
},
"request": [Function],
Expand All @@ -67,6 +70,7 @@ describe('config', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": 10000,
"type": "http",
},
"request": [Function],
Expand All @@ -88,6 +92,7 @@ describe('config', () => {
"request": [Function],
"retryCount": 3,
"retryDelay": 150,
"timeout": 10000,
"type": "http",
},
"request": [Function],
Expand Down Expand Up @@ -172,6 +177,34 @@ describe('request', () => {
`)
expect(end > 500 && end < 520).toBeTruthy()
})

test('behavior: timeout', async () => {
const server = await createHttpServer(async (req, res) => {
await wait(5000)
res.writeHead(500, {
'Content-Type': 'application/json',
})
res.end(JSON.stringify({}))
})

const transport = http(server.url, {
key: 'jsonRpc',
name: 'JSON RPC',
timeout: 100,
})({ chain: localhost })

await expect(() =>
transport.request({ method: 'eth_blockNumber' }),
).rejects.toThrowErrorMatchingInlineSnapshot(`
"The request took too long to respond.
URL: http://localhost
Request body: {\\"method\\":\\"eth_blockNumber\\"}
Details: The request timed out.
Version: [email protected]"
`)
})
})

test('no url', () => {
Expand Down
Loading

2 comments on commit db9caa9

@vercel
Copy link

@vercel vercel bot commented on db9caa9 Feb 20, 2023

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:

viem-site – ./site

viem-site.vercel.app
viem-site-git-main-wagmi-dev.vercel.app
viem-site-wagmi-dev.vercel.app

@vercel
Copy link

@vercel vercel bot commented on db9caa9 Feb 20, 2023

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:

viem-playground – ./playgrounds/browser

viem-playground.vercel.app
viem-playground-git-main-wagmi-dev.vercel.app
viem-playground-wagmi-dev.vercel.app

Please sign in to comment.