diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c0af562..ea26558 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,16 +6,16 @@ jobs: strategy: fail-fast: false matrix: - node-version: [18, 20] + node-version: [20.19.0, 22.12.0] name: Node.js v${{ matrix.node-version }} steps: - name: Setup Node.js - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Install Bun uses: oven-sh/setup-bun@v1 diff --git a/package.json b/package.json index a87fe6c..01dd9b6 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,12 @@ "private": true, "version": "0.0.1", "type": "module", + "engines": { + "node": ">=20.19.0 || >=22.12.0" + }, "scripts": { "dev": "vite dev", - "build": "vite build", + "build": "svelte-kit sync && vite build", "preview": "vite preview", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", diff --git a/src/lib/lookup.ts b/src/lib/lookup.ts index a0153a7..d1b8899 100644 --- a/src/lib/lookup.ts +++ b/src/lib/lookup.ts @@ -1,39 +1,54 @@ import {APIClient} from '@wharfkit/antelope' -import type {API, PublicKey} from '@wharfkit/antelope' +import type {PublicKey} from '@wharfkit/antelope' import type {PermissionLevelType} from '@wharfkit/antelope' import type {Chain} from './chains' import {logger} from './utils/logger' -export const networkRequest = ( +// Keep chain lookup timeout behavior aligned with previous production behavior. +const CHAIN_LOOKUP_TIMEOUT_MS = 2000 + +const withTimeout = async (promise: Promise, timeoutMs: number): Promise => { + let timeoutId: ReturnType | undefined + const timeoutPromise = new Promise((_resolve, reject) => { + timeoutId = setTimeout(() => { + reject(new Error('Request timed out.')) + }, timeoutMs) + }) + + try { + return await Promise.race([promise, timeoutPromise]) + } finally { + if (timeoutId) { + clearTimeout(timeoutId) + } + } +} + +const chainLookup = async ( publicKey: PublicKey, chain: Chain, apiClient?: APIClient ): Promise => { - return new Promise((resolve, reject) => { - const client = apiClient || new APIClient(chain) + const client = apiClient || new APIClient(chain) + const response = await withTimeout( + client.v1.chain.get_accounts_by_authorizers({ + keys: [publicKey], + }), + CHAIN_LOOKUP_TIMEOUT_MS + ) - const timeoutId = setTimeout(() => { - reject(new Error('Request timed out.')) - }, 2000) + return response.accounts.map((account) => ({ + actor: account.account_name, + permission: account.permission_name, + })) +} - client.v1.chain - .get_accounts_by_authorizers({ - keys: [publicKey], - }) - .then((response: API.v1.AccountsByAuthorizers) => { - clearTimeout(timeoutId) - resolve( - response.accounts.map((account) => ({ - actor: account.account_name, - permission: account.permission_name, - })) - ) - }) - .catch((error) => { - clearTimeout(timeoutId) - reject(error) - }) - }) +export const networkRequest = async ( + publicKey: PublicKey, + chain: Chain, + apiClient?: APIClient +): Promise => { + return await chainLookup(publicKey, chain, apiClient) } export const lookupNetwork = async (publicKey: PublicKey, chain: Chain, apiClient?: APIClient) => { diff --git a/tests/index.test.ts b/tests/index.test.ts index cd0a4b0..ee16b81 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -38,7 +38,7 @@ describe('lookupNetwork', () => { const result = await lookupNetwork( publicKey, chain, - makeClient('https://jungle0.greymass.com') + makeClient('https://jungle9.greymass.com') ) assert.containsAllKeys(result, ['chain', 'accounts']) assert.equal(result.chain.name, chain.name)