From 99bcbdb89ba16d4cd9f5dd70f3c179e1cb4cc212 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Thu, 28 Dec 2023 12:56:14 +0100 Subject: [PATCH] fix: minor issues (#297) * 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 --- package.json | 10 ++++++---- src/avatar/auto.js | 23 +++++++++++------------ src/avatar/provider.js | 5 +---- src/index.js | 2 +- src/providers/reddit.js | 2 +- src/providers/youtube.js | 2 +- test/providers.js | 10 +++++----- 7 files changed, 26 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index a8ebaf6..4945c1a 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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)\"", @@ -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" }, diff --git a/src/avatar/auto.js b/src/avatar/auto.js index 8d1eb42..8c880e2 100644 --- a/src/avatar/auto.js +++ b/src/avatar/auto.js @@ -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)) { @@ -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) } diff --git a/src/avatar/provider.js b/src/avatar/provider.js index 628c74e..548a308 100644 --- a/src/avatar/provider.js +++ b/src/avatar/provider.js @@ -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) diff --git a/src/index.js b/src/index.js index ce3ff07..8d2541d 100644 --- a/src/index.js +++ b/src/index.js @@ -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)) }) ) ) diff --git a/src/providers/reddit.js b/src/providers/reddit.js index 64b8ec8..59ae43f 100644 --- a/src/providers/reddit.js +++ b/src/providers/reddit.js @@ -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 = { diff --git a/src/providers/youtube.js b/src/providers/youtube.js index b7a1ba1..890c904 100644 --- a/src/providers/youtube.js +++ b/src/providers/youtube.js @@ -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 diff --git a/test/providers.js b/test/providers.js index 2b54433..a06bd80 100644 --- a/test/providers.js +++ b/test/providers.js @@ -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 @@ -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')) @@ -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