Skip to content

Commit

Permalink
fix: add p-limit for avatar fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 2, 2024
1 parent a387c45 commit b8ee34d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"eslint": "^9.1.1",
"esno": "^4.7.0",
"jiti": "^1.21.0",
"p-limit": "^5.0.0",
"typescript": "^5.4.5",
"unbuild": "^2.0.0",
"vite": "^5.2.11",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions src/processing/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { $fetch } from 'ofetch'
import DatauriParser from 'datauri/parser'
import sharp from 'sharp'
import { consola } from 'consola'
import pLimit from 'p-limit'
import { version } from '../../package.json'
import type { SponsorkitConfig, Sponsorship } from '../types'

export async function resolveAvatars(
Expand All @@ -20,10 +22,17 @@ export async function resolveAvatars(

const fallbackDataUri = fallbackAvatar && pngToDataUri(await round(fallbackAvatar, 0.5, 100))

return Promise.all(ships.map(async (ship) => {
const limit = pLimit(15)

return Promise.all(ships.map(ship => limit(async () => {
const data = ship.privacyLevel === 'PRIVATE'
? fallbackAvatar
: await $fetch(ship.sponsor.avatarUrl, { responseType: 'arrayBuffer' })
: await $fetch(ship.sponsor.avatarUrl, {
responseType: 'arrayBuffer',
headers: {
'User-Agent': `Mozilla/5.0 Chrome/124.0.0.0 Safari/537.36 Sponsorkit/${version}`,
},
})
.catch((e) => {
t.error(`Failed to fetch avatar for ${ship.sponsor.login || ship.sponsor.name} [${ship.sponsor.avatarUrl}]`)
t.error(e)
Expand All @@ -42,7 +51,7 @@ export async function resolveAvatars(
ship.sponsor.avatarUrlMediumRes = pngToDataUri(await round(data, radius, 80))
ship.sponsor.avatarUrlLowRes = pngToDataUri(await round(data, radius, 50))
}
}))
})))
}

function toBuffer(ab: ArrayBuffer) {
Expand Down

0 comments on commit b8ee34d

Please sign in to comment.