viem 2.48.11 → 2.49.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/_cjs/actions/public/call.js +26 -6
  3. package/_cjs/actions/public/call.js.map +1 -1
  4. package/_cjs/clients/transports/http.js +19 -2
  5. package/_cjs/clients/transports/http.js.map +1 -1
  6. package/_cjs/errors/utils.js +17 -0
  7. package/_cjs/errors/utils.js.map +1 -1
  8. package/_cjs/errors/version.js +1 -1
  9. package/_cjs/errors/version.js.map +1 -1
  10. package/_cjs/index.js.map +1 -1
  11. package/_cjs/tempo/actions/wallet.js.map +1 -1
  12. package/_cjs/utils/buildRequest.js +12 -2
  13. package/_cjs/utils/buildRequest.js.map +1 -1
  14. package/_cjs/utils/ccip.js +17 -4
  15. package/_cjs/utils/ccip.js.map +1 -1
  16. package/_cjs/utils/promise/withRetry.js +23 -3
  17. package/_cjs/utils/promise/withRetry.js.map +1 -1
  18. package/_cjs/utils/promise/withTimeout.js +5 -2
  19. package/_cjs/utils/promise/withTimeout.js.map +1 -1
  20. package/_cjs/utils/rpc/http.js +5 -0
  21. package/_cjs/utils/rpc/http.js.map +1 -1
  22. package/_cjs/utils/wait.js +19 -2
  23. package/_cjs/utils/wait.js.map +1 -1
  24. package/_esm/actions/public/call.js +26 -6
  25. package/_esm/actions/public/call.js.map +1 -1
  26. package/_esm/clients/transports/http.js +19 -2
  27. package/_esm/clients/transports/http.js.map +1 -1
  28. package/_esm/errors/utils.js +15 -0
  29. package/_esm/errors/utils.js.map +1 -1
  30. package/_esm/errors/version.js +1 -1
  31. package/_esm/errors/version.js.map +1 -1
  32. package/_esm/index.js.map +1 -1
  33. package/_esm/tempo/actions/wallet.js +1 -1
  34. package/_esm/tempo/actions/wallet.js.map +1 -1
  35. package/_esm/utils/buildRequest.js +12 -2
  36. package/_esm/utils/buildRequest.js.map +1 -1
  37. package/_esm/utils/ccip.js +17 -4
  38. package/_esm/utils/ccip.js.map +1 -1
  39. package/_esm/utils/promise/withRetry.js +23 -3
  40. package/_esm/utils/promise/withRetry.js.map +1 -1
  41. package/_esm/utils/promise/withTimeout.js +5 -2
  42. package/_esm/utils/promise/withTimeout.js.map +1 -1
  43. package/_esm/utils/rpc/http.js +5 -0
  44. package/_esm/utils/rpc/http.js.map +1 -1
  45. package/_esm/utils/wait.js +19 -2
  46. package/_esm/utils/wait.js.map +1 -1
  47. package/_types/actions/public/call.d.ts +4 -1
  48. package/_types/actions/public/call.d.ts.map +1 -1
  49. package/_types/clients/transports/http.d.ts.map +1 -1
  50. package/_types/errors/utils.d.ts +3 -0
  51. package/_types/errors/utils.d.ts.map +1 -1
  52. package/_types/errors/version.d.ts +1 -1
  53. package/_types/errors/version.d.ts.map +1 -1
  54. package/_types/index.d.ts +1 -1
  55. package/_types/index.d.ts.map +1 -1
  56. package/_types/tempo/actions/wallet.d.ts +15 -5
  57. package/_types/tempo/actions/wallet.d.ts.map +1 -1
  58. package/_types/types/eip1193.d.ts +2 -0
  59. package/_types/types/eip1193.d.ts.map +1 -1
  60. package/_types/utils/buildRequest.d.ts +5 -3
  61. package/_types/utils/buildRequest.d.ts.map +1 -1
  62. package/_types/utils/ccip.d.ts +5 -3
  63. package/_types/utils/ccip.d.ts.map +1 -1
  64. package/_types/utils/promise/withRetry.d.ts +3 -2
  65. package/_types/utils/promise/withRetry.d.ts.map +1 -1
  66. package/_types/utils/promise/withTimeout.d.ts +1 -1
  67. package/_types/utils/promise/withTimeout.d.ts.map +1 -1
  68. package/_types/utils/rpc/http.d.ts +1 -1
  69. package/_types/utils/rpc/http.d.ts.map +1 -1
  70. package/_types/utils/wait.d.ts +3 -1
  71. package/_types/utils/wait.d.ts.map +1 -1
  72. package/actions/public/call.ts +59 -23
  73. package/clients/transports/http.ts +18 -2
  74. package/errors/utils.ts +19 -0
  75. package/errors/version.ts +1 -1
  76. package/index.ts +1 -0
  77. package/package.json +1 -1
  78. package/tempo/actions/wallet.ts +15 -5
  79. package/types/eip1193.ts +2 -0
  80. package/utils/buildRequest.ts +22 -6
  81. package/utils/ccip.ts +22 -4
  82. package/utils/promise/withRetry.ts +29 -2
  83. package/utils/promise/withTimeout.ts +6 -3
  84. package/utils/rpc/http.ts +7 -1
  85. package/utils/wait.ts +24 -2
@@ -1,4 +1,8 @@
1
- import type { ErrorType } from '../../errors/utils.js'
1
+ import {
2
+ type ErrorType,
3
+ getAbortError,
4
+ isAbortError,
5
+ } from '../../errors/utils.js'
2
6
  import { wait } from '../wait.js'
3
7
 
4
8
  export type WithRetryParameters = {
@@ -19,6 +23,8 @@ export type WithRetryParameters = {
19
23
  error: Error
20
24
  }) => Promise<boolean> | boolean)
21
25
  | undefined
26
+ // AbortSignal to cancel retries.
27
+ signal?: AbortSignal | undefined
22
28
  }
23
29
 
24
30
  export type WithRetryErrorType = ErrorType
@@ -29,14 +35,27 @@ export function withRetry<data>(
29
35
  delay: delay_ = 100,
30
36
  retryCount = 2,
31
37
  shouldRetry = () => true,
38
+ signal,
32
39
  }: WithRetryParameters = {},
33
40
  ) {
34
41
  return new Promise<data>((resolve, reject) => {
35
42
  const attemptRetry = async ({ count = 0 } = {}) => {
43
+ if (signal?.aborted) {
44
+ reject(getAbortError(signal))
45
+ return
46
+ }
47
+
36
48
  const retry = async ({ error }: { error: Error }) => {
37
49
  const delay =
38
50
  typeof delay_ === 'function' ? delay_({ count, error }) : delay_
39
- if (delay) await wait(delay)
51
+ if (delay) {
52
+ try {
53
+ await wait(delay, { signal })
54
+ } catch (err) {
55
+ reject(err)
56
+ return
57
+ }
58
+ }
40
59
  attemptRetry({ count: count + 1 })
41
60
  }
42
61
 
@@ -44,6 +63,14 @@ export function withRetry<data>(
44
63
  const data = await fn()
45
64
  resolve(data)
46
65
  } catch (err) {
66
+ if (signal?.aborted) {
67
+ reject(getAbortError(signal))
68
+ return
69
+ }
70
+ if (isAbortError(err)) {
71
+ reject(err)
72
+ return
73
+ }
47
74
  if (
48
75
  count < retryCount &&
49
76
  (await shouldRetry({ count, error: err as Error }))
@@ -1,4 +1,4 @@
1
- import type { ErrorType } from '../../errors/utils.js'
1
+ import { type ErrorType, isAbortError } from '../../errors/utils.js'
2
2
 
3
3
  export type WithTimeoutErrorType = ErrorType
4
4
 
@@ -24,8 +24,8 @@ export function withTimeout<data>(
24
24
  return new Promise((resolve, reject) => {
25
25
  ;(async () => {
26
26
  let timeoutId!: NodeJS.Timeout
27
+ const controller = new AbortController()
27
28
  try {
28
- const controller = new AbortController()
29
29
  if (timeout > 0) {
30
30
  timeoutId = setTimeout(() => {
31
31
  if (signal) {
@@ -37,7 +37,10 @@ export function withTimeout<data>(
37
37
  }
38
38
  resolve(await fn({ signal: controller?.signal || null }))
39
39
  } catch (err) {
40
- if ((err as Error)?.name === 'AbortError') reject(errorInstance)
40
+ if (controller?.signal.aborted && isAbortError(err)) {
41
+ reject(errorInstance)
42
+ return
43
+ }
41
44
  reject(err)
42
45
  } finally {
43
46
  clearTimeout(timeoutId)
package/utils/rpc/http.ts CHANGED
@@ -4,7 +4,11 @@ import {
4
4
  TimeoutError,
5
5
  type TimeoutErrorType,
6
6
  } from '../../errors/request.js'
7
- import type { ErrorType } from '../../errors/utils.js'
7
+ import {
8
+ type ErrorType,
9
+ getAbortError,
10
+ isAbortError,
11
+ } from '../../errors/utils.js'
8
12
  import type { RpcRequest, RpcResponse } from '../../types/rpc.js'
9
13
  import type { MaybePromise } from '../../types/utils.js'
10
14
  import {
@@ -174,6 +178,8 @@ export function getHttpRpcClient(
174
178
 
175
179
  return data
176
180
  } catch (err) {
181
+ if (signal_?.aborted) throw getAbortError(signal_)
182
+ if (isAbortError(err)) throw err
177
183
  if (err instanceof HttpRequestError) throw err
178
184
  if (err instanceof TimeoutError) throw err
179
185
  throw new HttpRequestError({
package/utils/wait.ts CHANGED
@@ -1,3 +1,25 @@
1
- export async function wait(time: number) {
2
- return new Promise((res) => setTimeout(res, time))
1
+ import { getAbortError } from '../errors/utils.js'
2
+
3
+ export async function wait(
4
+ time: number,
5
+ { signal }: { signal?: AbortSignal | undefined } = {},
6
+ ) {
7
+ return new Promise<void>((resolve, reject) => {
8
+ if (signal?.aborted) {
9
+ reject(getAbortError(signal))
10
+ return
11
+ }
12
+
13
+ const cleanup = () => signal?.removeEventListener('abort', onAbort)
14
+ const timeout = setTimeout(() => {
15
+ cleanup()
16
+ resolve()
17
+ }, time)
18
+ const onAbort = () => {
19
+ clearTimeout(timeout)
20
+ cleanup()
21
+ reject(getAbortError(signal))
22
+ }
23
+ signal?.addEventListener('abort', onAbort, { once: true })
24
+ })
3
25
  }