From bd7662e5e4a571ebaa41f26ee97d5f67f7d5dc93 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 4 Feb 2022 11:10:53 +0900 Subject: [PATCH] refactor --- packages/backend/src/mfm/from-html.ts | 4 +--- packages/backend/src/misc/before-shutdown.ts | 14 ++++++++------ .../queue/processors/db/import-custom-emojis.ts | 4 +++- packages/backend/src/queue/processors/inbox.ts | 8 +++++--- .../src/remote/activitypub/kernel/announce/note.ts | 11 +++++++---- .../src/remote/activitypub/kernel/flag/index.ts | 2 +- .../backend/src/remote/activitypub/kernel/index.ts | 6 ++++-- 7 files changed, 29 insertions(+), 20 deletions(-) diff --git a/packages/backend/src/mfm/from-html.ts b/packages/backend/src/mfm/from-html.ts index fc6d3b3062..21e5ebb7a1 100644 --- a/packages/backend/src/mfm/from-html.ts +++ b/packages/backend/src/mfm/from-html.ts @@ -5,9 +5,7 @@ import { URL } from 'url'; const urlRegex = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+/; const urlRegexFull = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+$/; -export function fromHtml(html: string, hashtagNames?: string[]): string | null { - if (html == null) return null; - +export function fromHtml(html: string, hashtagNames?: string[]): string { const dom = parse5.parseFragment(html); let text = ''; diff --git a/packages/backend/src/misc/before-shutdown.ts b/packages/backend/src/misc/before-shutdown.ts index 33abf5fb4d..93ac7a1f39 100644 --- a/packages/backend/src/misc/before-shutdown.ts +++ b/packages/backend/src/misc/before-shutdown.ts @@ -24,14 +24,14 @@ const SHUTDOWN_TIMEOUT = 15000; * down the process. * @type {BeforeShutdownListener[]} */ -const shutdownListeners = []; +const shutdownListeners: ((signalOrEvent: string) => void)[] = []; /** * Listen for signals and execute given `fn` function once. * @param {string[]} signals System signals to listen to. * @param {function(string)} fn Function to execute on shutdown. */ -const processOnce = (signals, fn) => { +const processOnce = (signals: string[], fn: (signalOrEvent: string) => void) => { for (const sig of signals) { process.once(sig, fn); } @@ -41,7 +41,7 @@ const processOnce = (signals, fn) => { * Sets a forced shutdown mechanism that will exit the process after `timeout` milliseconds. * @param {number} timeout Time to wait before forcing shutdown (milliseconds) */ -const forceExitAfter = timeout => () => { +const forceExitAfter = (timeout: number) => () => { setTimeout(() => { // Force shutdown after timeout console.warn(`Could not close resources gracefully after ${timeout}ms: forcing shutdown`); @@ -55,7 +55,7 @@ const forceExitAfter = timeout => () => { * be logged out as a warning, but won't prevent other callbacks from executing. * @param {string} signalOrEvent The exit signal or event name received on the process. */ -async function shutdownHandler(signalOrEvent) { +async function shutdownHandler(signalOrEvent: string) { if (process.env.NODE_ENV === 'test') return process.exit(0); console.warn(`Shutting down: received [${signalOrEvent}] signal`); @@ -64,7 +64,9 @@ async function shutdownHandler(signalOrEvent) { try { await listener(signalOrEvent); } catch (err) { - console.warn(`A shutdown handler failed before completing with: ${err.message || err}`); + if (err instanceof Error) { + console.warn(`A shutdown handler failed before completing with: ${err.message || err}`); + } } } @@ -78,7 +80,7 @@ async function shutdownHandler(signalOrEvent) { * @param {BeforeShutdownListener} listener The shutdown listener to register. * @returns {BeforeShutdownListener} Echoes back the supplied `listener`. */ -export function beforeShutdown(listener) { +export function beforeShutdown(listener: () => void) { shutdownListeners.push(listener); return listener; } diff --git a/packages/backend/src/queue/processors/db/import-custom-emojis.ts b/packages/backend/src/queue/processors/db/import-custom-emojis.ts index 04e93671ed..b6c0126534 100644 --- a/packages/backend/src/queue/processors/db/import-custom-emojis.ts +++ b/packages/backend/src/queue/processors/db/import-custom-emojis.ts @@ -41,7 +41,9 @@ export async function importCustomEmojis(job: Bull.Job, don fs.writeFileSync(destPath, '', 'binary'); await downloadUrl(file.url, destPath); } catch (e) { // TODO: 何度か再試行 - logger.error(e); + if (e instanceof Error || typeof e === 'string') { + logger.error(e); + } throw e; } diff --git a/packages/backend/src/queue/processors/inbox.ts b/packages/backend/src/queue/processors/inbox.ts index bfdebc0077..82a0de9663 100644 --- a/packages/backend/src/queue/processors/inbox.ts +++ b/packages/backend/src/queue/processors/inbox.ts @@ -54,10 +54,12 @@ export default async (job: Bull.Job): Promise => { authUser = await dbResolver.getAuthUserFromApId(getApId(activity.actor)); } catch (e) { // 対象が4xxならスキップ - if (e instanceof StatusError && e.isClientError) { - return `skip: Ignored deleted actors on both ends ${activity.actor} - ${e.statusCode}`; + if (e instanceof StatusError) { + if (e.isClientError) { + return `skip: Ignored deleted actors on both ends ${activity.actor} - ${e.statusCode}`; + } + throw `Error in actor ${activity.actor} - ${e.statusCode || e}`; } - throw `Error in actor ${activity.actor} - ${e.statusCode || e}`; } } diff --git a/packages/backend/src/remote/activitypub/kernel/announce/note.ts b/packages/backend/src/remote/activitypub/kernel/announce/note.ts index e9158f7752..eae92d4180 100644 --- a/packages/backend/src/remote/activitypub/kernel/announce/note.ts +++ b/packages/backend/src/remote/activitypub/kernel/announce/note.ts @@ -42,11 +42,14 @@ export default async function(resolver: Resolver, actor: IRemoteUser, activity: renote = await resolveNote(targetUri); } catch (e) { // 対象が4xxならスキップ - if (e instanceof StatusError && e.isClientError) { - logger.warn(`Ignored announce target ${targetUri} - ${e.statusCode}`); - return; + if (e instanceof StatusError) { + if (e.isClientError) { + logger.warn(`Ignored announce target ${targetUri} - ${e.statusCode}`); + return; + } + + logger.warn(`Error in announce target ${targetUri} - ${e.statusCode || e}`); } - logger.warn(`Error in announce target ${targetUri} - ${e.statusCode || e}`); throw e; } diff --git a/packages/backend/src/remote/activitypub/kernel/flag/index.ts b/packages/backend/src/remote/activitypub/kernel/flag/index.ts index aec6d2daaa..d910e2ebe2 100644 --- a/packages/backend/src/remote/activitypub/kernel/flag/index.ts +++ b/packages/backend/src/remote/activitypub/kernel/flag/index.ts @@ -10,7 +10,7 @@ export default async (actor: IRemoteUser, activity: IFlag): Promise => { // 対象ユーザーは一番最初のユーザー として あとはコメントとして格納する const uris = getApIds(activity.object); - const userIds = uris.filter(uri => uri.startsWith(config.url + '/users/')).map(uri => uri.split('/').pop()); + const userIds = uris.filter(uri => uri.startsWith(config.url + '/users/')).map(uri => uri.split('/').pop()!); const users = await Users.find({ id: In(userIds), }); diff --git a/packages/backend/src/remote/activitypub/kernel/index.ts b/packages/backend/src/remote/activitypub/kernel/index.ts index 20df28eec6..a103e5a1be 100644 --- a/packages/backend/src/remote/activitypub/kernel/index.ts +++ b/packages/backend/src/remote/activitypub/kernel/index.ts @@ -25,8 +25,10 @@ export async function performActivity(actor: IRemoteUser, activity: IObject) { const act = await resolver.resolve(item); try { await performOneActivity(actor, act); - } catch (e) { - apLogger.error(e); + } catch (err) { + if (err instanceof Error || typeof err === 'string') { + apLogger.error(err); + } } } } else {