Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
65 changes: 40 additions & 25 deletions src/lib/lookup.ts
Original file line number Diff line number Diff line change
@@ -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 <T>(promise: Promise<T>, timeoutMs: number): Promise<T> => {
let timeoutId: ReturnType<typeof setTimeout> | undefined
const timeoutPromise = new Promise<T>((_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<PermissionLevelType[]> => {
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<PermissionLevelType[]> => {
return await chainLookup(publicKey, chain, apiClient)
}

export const lookupNetwork = async (publicKey: PublicKey, chain: Chain, apiClient?: APIClient) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down