Skip to content

Commit

Permalink
fix: minor issues (#297)
Browse files Browse the repository at this point in the history
* fix: cleanup chrome process

* fix: associate provider name with errors

* fix: function name

* refactor: explicit error message

* build: downgrade ava

* fix(reddit): avatar css selector

* refactor: always attach provider name with errors

* test: make tests great again

* ci: disable worker threads

* ci: skip test
  • Loading branch information
Kikobeats authored Dec 28, 2023
1 parent 925df9b commit 99bcbdb
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 28 deletions.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"@commitlint/config-conventional": "latest",
"@ksmithut/prettier-standard": "latest",
"async-listen": "latest",
"ava": "latest",
"ava": "5",
"browser-sync": "latest",
"concurrently": "latest",
"conventional-github-releaser": "latest",
Expand Down Expand Up @@ -168,7 +168,7 @@
"clean": "rm -rf node_modules",
"contributors": "(npx git-authors-cli && npx finepack --sort-ignore-object-at ava && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
"deploy": "now -e NODE_ENV=production --public && now alias && now rm unavatar --safe --yes",
"dev": "TZ=UTC watchexec --clear --on-busy-update=restart node src/server.js",
"dev": "TZ=UTC watchexec --clear=clear --on-busy-update=restart 'fkill \"Google Chrome for Testing\" --silent && node src/server.js'",
"dev:docker": "docker build --platform linux/amd64 -t unavatar . && docker run --platform linux/amd64 --name unavatar -e NODE_ENV=staging -p 3000:3000 --rm unavatar",
"dev:docs": "concurrently \"npm run dev:docs:server\" \"npm run dev:docs:src\"",
"dev:docs:server": "browser-sync start public --server public --files \"index.html, README.md, public/**/*.(css|js)\"",
Expand All @@ -190,9 +190,11 @@
"private": true,
"license": "MIT",
"ava": {
"workerThreads": false,
"serial": true,
"files": [
"!test/helpers.js",
"test/**/*.js"
"test/**/*.js",
"!test/helpers.js"
],
"timeout": "1m"
},
Expand Down
23 changes: 11 additions & 12 deletions src/avatar/auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ const is = ({ input }) => {

const getAvatarContent = name => async input => {
if (typeof input !== 'string' || input === '') {
throw new ExtendableError({
name,
message: `Avatar \`${input}\` is invalid.`,
statusCode: 400
})
const message =
input === undefined ? 'not found' : `\`${input}\` is invalid`
const statusCode = input === undefined ? 404 : 400
throw new ExtendableError({ name, message, statusCode })
}

if (dataUriRegex().test(input)) {
Expand Down Expand Up @@ -55,27 +54,27 @@ const getAvatarContent = name => async input => {
return { type: 'url', data: url }
}

const getAvatar = async (fn, ...args) => {
const promise = Promise.resolve(fn(...args))
.then(getAvatarContent(fn.name))
const getAvatar = async (fn, name, args) => {
const promise = Promise.resolve(fn(args))
.then(getAvatarContent(name))
.catch(error => {
isIterable.forEach(error, error => {
error.statusCode = error.statusCode ?? error.response?.statusCode
error.name = fn.name
error.name = name
})
throw error
})

return pTimeout(promise, AVATAR_TIMEOUT).catch(error => {
error.name = fn.name
error.name = name
throw error
})
}

module.exports = async args => {
const collection = providersBy[is(args)]
const promises = collection.map(providerName =>
pTimeout(getAvatar(providers[providerName], args), AVATAR_TIMEOUT)
const promises = collection.map(name =>
pTimeout(getAvatar(providers[name], name, args), AVATAR_TIMEOUT)
)
return pAny(promises)
}
Expand Down
5 changes: 1 addition & 4 deletions src/avatar/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@

const { getAvatar } = require('./auto')

module.exports =
fn =>
async (...args) =>
getAvatar(fn, ...args)
module.exports = (name, fn) => args => getAvatar(fn, name, args)
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ router

forEach(providers, (fn, provider) =>
router.get(`/${provider}/:key`, (req, res) =>
ssrCache({ req, res, fn: avatar.resolve(avatar.provider(fn)) })
ssrCache({ req, res, fn: avatar.resolve(avatar.provider(provider, fn)) })
)
)

Expand Down
2 changes: 1 addition & 1 deletion src/providers/reddit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = PCancelable.fn(async function reddit ({ input }, onCancel) {
onCancel(() => promise.onCancel())
const { html } = await promise
const $ = cheerio.load(html)
return $('img[alt="User avatar"]').attr('src')
return $('img[alt*="avatar"]').attr('src')
})

module.exports.supported = {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const cheerio = require('cheerio')

const getHTML = require('../util/html-get')

module.exports = PCancelable.fn(async function gitlab ({ input }, onCancel) {
module.exports = PCancelable.fn(async function youtube ({ input }, onCancel) {
const promise = getHTML(`https://www.youtube.com/@${input}`)
onCancel(() => promise.onCancel())
const { html } = await promise
Expand Down
10 changes: 5 additions & 5 deletions test/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ test('substack', async t => {
})
t.true(body.url.includes('images.weserv.nl'))
})

test('reddit', async t => {
//
;(isCI ? test.skip : test)('reddit', async t => {
const serverUrl = await runServer(t)
const { body } = await got('reddit/kikobeats?json', {
prefixUrl: serverUrl
Expand All @@ -121,7 +121,7 @@ test('reddit', async t => {

test('microlink', async t => {
const serverUrl = await runServer(t)
const { body } = await got('microlink/reddit.com?json', {
const { body } = await got('microlink/teslahunt.io?json', {
prefixUrl: serverUrl
})
t.true(body.url.includes('images.weserv.nl'))
Expand All @@ -134,8 +134,8 @@ test('readcv', async t => {
})
t.true(body.url.includes('images.weserv.nl'))
})

test('tiktok', async t => {
//
;(isCI ? test.skip : test)('tiktok', async t => {
const serverUrl = await runServer(t)
const { body } = await got('tiktok/carlosazaustre?json', {
prefixUrl: serverUrl
Expand Down

0 comments on commit 99bcbdb

Please sign in to comment.