From b8fa7124d1d1e4c9c16b60ad2ce58559f72efd27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Wed, 27 Sep 2023 09:38:25 +0200 Subject: [PATCH] fix: timer types to be context dependent (#6460) * fix: infer Timer and Timeout types depending on context instead of forcing nodejs types Signed-off-by: Marin Petrunic * fix: lint Signed-off-by: Marin Petrunic --------- Signed-off-by: Marin Petrunic --- .../test/unit/web3_batch_request.test.ts | 4 ++-- .../src/utils/reject_if_block_timeout.ts | 2 +- .../src/utils/wait_for_transaction_receipt.ts | 2 +- .../web3-utils/src/chunk_response_parser.ts | 3 ++- packages/web3-utils/src/promise_helpers.ts | 20 +++++++++++-------- .../web3-utils/src/web3_deferred_promise.ts | 3 ++- 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/packages/web3-core/test/unit/web3_batch_request.test.ts b/packages/web3-core/test/unit/web3_batch_request.test.ts index 7f5fc20ee24..cc861276a5f 100644 --- a/packages/web3-core/test/unit/web3_batch_request.test.ts +++ b/packages/web3-core/test/unit/web3_batch_request.test.ts @@ -16,7 +16,7 @@ along with web3.js. If not, see . */ import { JsonRpcBatchRequest, JsonRpcBatchResponse, JsonRpcOptionalRequest } from 'web3-types'; -import { jsonRpc, Web3DeferredPromise } from 'web3-utils'; +import { jsonRpc, Web3DeferredPromise, Timeout } from 'web3-utils'; import { OperationAbortError, OperationTimeoutError } from 'web3-errors'; import { Web3BatchRequest } from '../../src/web3_batch_request'; @@ -174,7 +174,7 @@ describe('Web3BatchRequest', () => { }); it('should timeout if request not executed in a particular time', async () => { - let timerId!: NodeJS.Timeout; + let timerId!: Timeout; jest.spyOn(requestManager, 'sendBatch').mockImplementation(async () => { return new Promise(resolve => { diff --git a/packages/web3-eth/src/utils/reject_if_block_timeout.ts b/packages/web3-eth/src/utils/reject_if_block_timeout.ts index 3f7faa7e0f1..2019ce3380d 100644 --- a/packages/web3-eth/src/utils/reject_if_block_timeout.ts +++ b/packages/web3-eth/src/utils/reject_if_block_timeout.ts @@ -34,7 +34,7 @@ function resolveByPolling( transactionHash?: Bytes, ): [Promise, ResourceCleaner] { const pollingInterval = web3Context.transactionPollingInterval; - const [intervalId, promiseToError]: [NodeJS.Timer, Promise] = + const [intervalId, promiseToError] = rejectIfConditionAtInterval(async () => { let lastBlockNumber; try { diff --git a/packages/web3-eth/src/utils/wait_for_transaction_receipt.ts b/packages/web3-eth/src/utils/wait_for_transaction_receipt.ts index 1809654bc1a..54ff06f588b 100644 --- a/packages/web3-eth/src/utils/wait_for_transaction_receipt.ts +++ b/packages/web3-eth/src/utils/wait_for_transaction_receipt.ts @@ -43,7 +43,7 @@ export async function waitForTransactionReceipt } }, pollingInterval); - const [timeoutId, rejectOnTimeout]: [NodeJS.Timer, Promise] = rejectIfTimeout( + const [timeoutId, rejectOnTimeout] = rejectIfTimeout( web3Context.transactionPollingTimeout, new TransactionPollingTimeoutError({ numberOfSeconds: web3Context.transactionPollingTimeout / 1000, diff --git a/packages/web3-utils/src/chunk_response_parser.ts b/packages/web3-utils/src/chunk_response_parser.ts index f4949953e74..d9b37c7b6ee 100644 --- a/packages/web3-utils/src/chunk_response_parser.ts +++ b/packages/web3-utils/src/chunk_response_parser.ts @@ -17,10 +17,11 @@ along with web3.js. If not, see . import { JsonRpcResponse } from 'web3-types'; import { InvalidResponseError } from 'web3-errors'; import { EventEmitter } from 'events'; +import { Timeout } from './promise_helpers.js'; export class ChunkResponseParser { private lastChunk: string | undefined; - private lastChunkTimeout: NodeJS.Timeout | undefined; + private lastChunkTimeout: Timeout | undefined; private _clearQueues: (() => void) | undefined; private readonly eventEmitter: EventEmitter; private readonly autoReconnect: boolean; diff --git a/packages/web3-utils/src/promise_helpers.ts b/packages/web3-utils/src/promise_helpers.ts index 43a3ade298c..c968ce247e9 100644 --- a/packages/web3-utils/src/promise_helpers.ts +++ b/packages/web3-utils/src/promise_helpers.ts @@ -17,6 +17,10 @@ along with web3.js. If not, see . import { isNullish } from 'web3-validator'; +export type Timer = ReturnType; +export type Timeout = ReturnType; + + /** * An alternative to the node function `isPromise` that exists in `util/types` because it is not available on the browser. * @param object - to check if it is a `Promise` @@ -54,7 +58,7 @@ export async function waitWithTimeout( timeout: number, error?: Error, ): Promise { - let timeoutId: NodeJS.Timeout | undefined; + let timeoutId: Timeout | undefined; const result = await Promise.race([ awaitable instanceof Promise ? awaitable : awaitable(), new Promise((resolve, reject) => { @@ -81,7 +85,7 @@ export async function pollTillDefined( ): Promise> { const awaitableRes = waitWithTimeout(func, interval); - let intervalId: NodeJS.Timer | undefined; + let intervalId: Timer | undefined; const polledRes = new Promise>((resolve, reject) => { intervalId = setInterval(() => { (async () => { @@ -122,14 +126,14 @@ export async function pollTillDefined( * const [timerId, promise] = web3.utils.rejectIfTimeout(100, new Error('time out')); * ``` */ -export function rejectIfTimeout(timeout: number, error: Error): [NodeJS.Timer, Promise] { - let timeoutId: NodeJS.Timer | undefined; +export function rejectIfTimeout(timeout: number, error: Error): [Timer, Promise] { + let timeoutId: Timer | undefined; const rejectOnTimeout = new Promise((_, reject) => { timeoutId = setTimeout(() => { reject(error); }, timeout); }); - return [timeoutId as unknown as NodeJS.Timer, rejectOnTimeout]; + return [timeoutId!, rejectOnTimeout]; } /** * Sets an interval that repeatedly executes the given cond function with the specified interval between each call. @@ -141,8 +145,8 @@ export function rejectIfTimeout(timeout: number, error: Error): [NodeJS.Timer, P export function rejectIfConditionAtInterval( cond: AsyncFunction, interval: number, -): [NodeJS.Timer, Promise] { - let intervalId: NodeJS.Timer | undefined; +): [Timer, Promise] { + let intervalId: Timer | undefined; const rejectIfCondition = new Promise((_, reject) => { intervalId = setInterval(() => { (async () => { @@ -154,5 +158,5 @@ export function rejectIfConditionAtInterval( })() as unknown; }, interval); }); - return [intervalId as unknown as NodeJS.Timer, rejectIfCondition]; + return [intervalId!, rejectIfCondition]; } diff --git a/packages/web3-utils/src/web3_deferred_promise.ts b/packages/web3-utils/src/web3_deferred_promise.ts index f1a46c8ef03..01805b774be 100644 --- a/packages/web3-utils/src/web3_deferred_promise.ts +++ b/packages/web3-utils/src/web3_deferred_promise.ts @@ -17,6 +17,7 @@ along with web3.js. If not, see . import { OperationTimeoutError } from 'web3-errors'; import { Web3DeferredPromiseInterface } from 'web3-types'; +import { Timeout } from './promise_helpers.js'; /** * The class is a simple implementation of a deferred promise with optional timeout functionality, @@ -32,7 +33,7 @@ export class Web3DeferredPromise implements Promise, Web3DeferredPromiseIn private _resolve!: (value: T | PromiseLike) => void; private _reject!: (reason?: unknown) => void; private _state: 'pending' | 'fulfilled' | 'rejected' = 'pending'; - private _timeoutId?: NodeJS.Timeout; + private _timeoutId?: Timeout; private readonly _timeoutInterval?: number; private readonly _timeoutMessage: string;