Skip to content

Commit

Permalink
Merge pull request #2 from greymass/adding-timeout
Browse files Browse the repository at this point in the history
Adding time out
  • Loading branch information
dafuga authored Jun 28, 2024
2 parents a84d3d6 + 7728306 commit 4f2a317
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

41 changes: 27 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {serve} from 'bun'
import {API, APIClient, PublicKey} from '@wharfkit/antelope'
import {API, APIClient, type PermissionLevelType, PublicKey} from '@wharfkit/antelope'
import {MAINNET_CHAINS, TESTNET_CHAINS} from './chains'
import type {Chain} from './types'

Expand Down Expand Up @@ -29,9 +29,6 @@ export const accountLookup = async (req: Request) => {
accounts,
}))

const totalAccounts = networkAccounts.reduce((total, {accounts}) => total + accounts.length, 0)
log(`Found ${totalAccounts} auth(s) across ${networkAccounts.length} network(s)`)

return new Response(JSON.stringify(networkAccounts))
}

Expand All @@ -45,16 +42,32 @@ export const lookupNetwork = async (publicKey: PublicKey, chain: Chain, apiClien
}
}

const networkRequest = async (publicKey: PublicKey, chain: Chain, apiClient?: APIClient) => {
const client = apiClient || new APIClient(chain)
const response: API.v1.AccountsByAuthorizers =
await client.v1.chain.get_accounts_by_authorizers({
keys: [publicKey],
})
return response.accounts.map((account) => ({
actor: account.account_name,
permission: account.permission_name,
}))
const networkRequest = (
publicKey: PublicKey,
chain: Chain,
apiClient?: APIClient
): Promise<PermissionLevelType[]> => {
return new Promise((resolve, reject) => {
const client = apiClient || new APIClient(chain)
client.v1.chain
.get_accounts_by_authorizers({
keys: [publicKey],
})
.then((response: API.v1.AccountsByAuthorizers) => {
resolve(
response.accounts.map((account) => ({
actor: account.account_name,
permission: account.permission_name,
}))
)
})
.catch((error) => {
reject(error)
})
setTimeout(() => {
reject('Request timed out.')
}, 600)
})
}

function log(message: string) {
Expand Down

0 comments on commit 4f2a317

Please sign in to comment.