From 0ac115874cca2f109da16ec0b9e8947dbba2071f Mon Sep 17 00:00:00 2001 From: naskya Date: Sat, 20 Jan 2024 08:27:08 +0900 Subject: [PATCH] refactor: bring all internal functions into misc/native-utils.ts Co-authored-by: sup39 --- .../native-utils/src/util/convert_host.rs | 14 +++++++----- packages/backend/src/boot/index.ts | 8 +++---- packages/backend/src/boot/master.ts | 8 +++---- packages/backend/src/config/index.ts | 3 --- .../backend/src/misc/check-hit-antenna.ts | 5 +++-- packages/backend/src/misc/native-utils.ts | 22 +++++++++++++++++++ packages/backend/src/misc/populate-emojis.ts | 3 ++- packages/backend/src/queue/index.ts | 4 ++-- .../src/remote/activitypub/kernel/read.ts | 3 ++- .../src/remote/activitypub/resolver.ts | 3 ++- packages/backend/src/server/activitypub.ts | 2 +- packages/backend/src/server/index.ts | 4 ++-- packages/backend/src/services/logger.ts | 8 +++---- 13 files changed, 57 insertions(+), 30 deletions(-) create mode 100644 packages/backend/src/misc/native-utils.ts diff --git a/packages/backend/native-utils/src/util/convert_host.rs b/packages/backend/native-utils/src/util/convert_host.rs index 9f3d47f44..7f9dc0e3b 100644 --- a/packages/backend/native-utils/src/util/convert_host.rs +++ b/packages/backend/native-utils/src/util/convert_host.rs @@ -1,20 +1,24 @@ -use crate::config::server::read_server_config; +use crate::config::server::ServerConfig; use idna; use url::Url; #[napi_derive::napi] -pub fn get_full_ap_account(username: String, host: Option) -> String { +pub fn get_full_ap_account( + server_config: ServerConfig, + username: String, + host: Option, +) -> String { if host.is_none() { - format!("{}@{}", username, extract_host(read_server_config().url)) + format!("{}@{}", username, extract_host(server_config.url)) } else { format!("{}@{}", username, to_puny(host.unwrap())) } } #[napi_derive::napi] -pub fn is_self_host(host: Option) -> bool { +pub fn is_self_host(server_config: ServerConfig, host: Option) -> bool { if let Some(x) = host { - extract_host(read_server_config().url) == to_puny(x) + extract_host(server_config.url) == to_puny(x) } else { true } diff --git a/packages/backend/src/boot/index.ts b/packages/backend/src/boot/index.ts index d4c47c5fe..75a6fa410 100644 --- a/packages/backend/src/boot/index.ts +++ b/packages/backend/src/boot/index.ts @@ -3,7 +3,7 @@ import chalk from "chalk"; import Xev from "xev"; import Logger from "@/services/logger.js"; -import { envOption } from "@/config/index.js"; +import { envConfig } from "@/misc/native-utils.js"; // for typeorm import "reflect-metadata"; @@ -26,14 +26,14 @@ export default async function () { const type = cluster.isPrimary ? "(master)" : "(worker)"; process.title = `Firefish ${mode} ${type}`; - if (cluster.isPrimary || envOption.disableClustering) { + if (cluster.isPrimary || envConfig.disableClustering) { await masterMain(); if (cluster.isPrimary) { ev.mount(); } } - if (cluster.isWorker || envOption.disableClustering) { + if (cluster.isWorker || envConfig.disableClustering) { await workerMain(); } @@ -75,7 +75,7 @@ cluster.on("exit", (worker) => { }); // Display detail of unhandled promise rejection -if (!envOption.quiet) { +if (!envConfig.quiet) { process.on("unhandledRejection", console.dir); } diff --git a/packages/backend/src/boot/master.ts b/packages/backend/src/boot/master.ts index f617c8594..f9a326f23 100644 --- a/packages/backend/src/boot/master.ts +++ b/packages/backend/src/boot/master.ts @@ -10,7 +10,7 @@ import semver from "semver"; import Logger from "@/services/logger.js"; import loadConfig from "@/config/load.js"; import type { Config } from "@/config/types.js"; -import { envOption } from "@/config/index.js"; +import { envConfig } from "@/misc/native-utils.js"; import { showMachineInfo } from "@/misc/show-machine-info.js"; import { db, initDb } from "@/db/postgre.js"; @@ -27,7 +27,7 @@ const bootLogger = logger.createSubLogger("boot", "magenta", false); const themeColor = chalk.hex("#31748f"); function greet() { - if (!envOption.quiet) { + if (!envConfig.quiet) { //#region Firefish logo console.log( themeColor( @@ -109,7 +109,7 @@ export async function masterMain() { bootLogger.succ("Firefish initialized"); - if (!envOption.disableClustering) { + if (!envConfig.disableClustering) { await spawnWorkers(config.clusterLimits); } @@ -120,7 +120,7 @@ export async function masterMain() { ); if ( - !envOption.noDaemons && + !envConfig.noDaemons && config.clusterLimits?.web && config.clusterLimits?.web >= 1 ) { diff --git a/packages/backend/src/config/index.ts b/packages/backend/src/config/index.ts index 7e8545015..523555008 100644 --- a/packages/backend/src/config/index.ts +++ b/packages/backend/src/config/index.ts @@ -1,5 +1,2 @@ import load from "./load.js"; -import { readEnvironmentConfig } from "native-utils/built/index.js"; - export default load(); -export const envOption = readEnvironmentConfig(); diff --git a/packages/backend/src/misc/check-hit-antenna.ts b/packages/backend/src/misc/check-hit-antenna.ts index 5e8f39601..c1689b9cb 100644 --- a/packages/backend/src/misc/check-hit-antenna.ts +++ b/packages/backend/src/misc/check-hit-antenna.ts @@ -2,7 +2,8 @@ import type { Antenna } from "@/models/entities/antenna.js"; import type { Note } from "@/models/entities/note.js"; import type { User } from "@/models/entities/user.js"; import { Blockings, Followings, UserProfiles } from "@/models/index.js"; -import { getFullApAccount, stringToAcct } from "native-utils/built/index.js"; +import { getFullApAccount } from "@/misc/native-utils.js"; +import { stringToAcct } from "native-utils/built/index.js"; import type { Packed } from "@/misc/schema.js"; import { Cache } from "@/misc/cache.js"; import { getWordHardMute } from "@/misc/check-word-mute.js"; @@ -25,7 +26,7 @@ export async function checkHitAntenna( if (antenna.src === "users") { const accts = antenna.users.map((x) => { const { username, host } = stringToAcct(x); - return getFullApAccount(username, host ?? null).toLowerCase(); + return getFullApAccount(username, host).toLowerCase(); }); if ( !accts.includes( diff --git a/packages/backend/src/misc/native-utils.ts b/packages/backend/src/misc/native-utils.ts new file mode 100644 index 000000000..1f73e6b9b --- /dev/null +++ b/packages/backend/src/misc/native-utils.ts @@ -0,0 +1,22 @@ +import { + readServerConfig, + readEnvironmentConfig, + getFullApAccount as getFullApAccountImpl, + isSelfHost as isSelfHostImpl, +} from "native-utils/built/index.js"; + +export const serverConfig = readServerConfig(); +export const envConfig = readEnvironmentConfig(); + +type Option = T | null | undefined; + +export function getFullApAccount( + username: string, + host: Option, +): string { + return getFullApAccountImpl(serverConfig, username, host); +} + +export function isSelfHost(host: Option): boolean { + return isSelfHostImpl(serverConfig, host); +} diff --git a/packages/backend/src/misc/populate-emojis.ts b/packages/backend/src/misc/populate-emojis.ts index ec533d549..8e8cd02e9 100644 --- a/packages/backend/src/misc/populate-emojis.ts +++ b/packages/backend/src/misc/populate-emojis.ts @@ -3,7 +3,8 @@ import { Emojis } from "@/models/index.js"; import type { Emoji } from "@/models/entities/emoji.js"; import type { Note } from "@/models/entities/note.js"; import { Cache } from "./cache.js"; -import { isSelfHost, toPunyOptional } from "native-utils/built/index.js"; +import { toPunyOptional } from "native-utils/built/index.js"; +import { isSelfHost } from "@/misc/native-utils.js"; import { decodeReaction } from "./reaction-lib.js"; import config from "@/config/index.js"; import { query } from "@/prelude/url.js"; diff --git a/packages/backend/src/queue/index.ts b/packages/backend/src/queue/index.ts index 3537866b8..1d6513ee6 100644 --- a/packages/backend/src/queue/index.ts +++ b/packages/backend/src/queue/index.ts @@ -5,7 +5,7 @@ import config from "@/config/index.js"; import type { DriveFile } from "@/models/entities/drive-file.js"; import type { IActivity } from "@/remote/activitypub/type.js"; import type { Webhook, webhookEventTypes } from "@/models/entities/webhook.js"; -import { envOption } from "@/config/index.js"; +import { envConfig } from "@/misc/native-utils.js"; import processDeliver from "./processors/deliver.js"; import processInbox from "./processors/inbox.js"; @@ -518,7 +518,7 @@ export function webhookDeliver( } export default function () { - if (envOption.onlyServer) return; + if (envConfig.onlyServer) return; deliverQueue.process(config.deliverJobConcurrency || 128, processDeliver); inboxQueue.process(config.inboxJobConcurrency || 16, processInbox); diff --git a/packages/backend/src/remote/activitypub/kernel/read.ts b/packages/backend/src/remote/activitypub/kernel/read.ts index 20f5d3128..fb5f5e809 100644 --- a/packages/backend/src/remote/activitypub/kernel/read.ts +++ b/packages/backend/src/remote/activitypub/kernel/read.ts @@ -1,7 +1,8 @@ import type { CacheableRemoteUser } from "@/models/entities/user.js"; import type { IRead } from "../type.js"; import { getApId } from "../type.js"; -import { isSelfHost, extractHost } from "native-utils/built/index.js"; +import { extractHost } from "native-utils/built/index.js"; +import { isSelfHost } from "@/misc/native-utils.js"; import { MessagingMessages } from "@/models/index.js"; import { readUserMessagingMessage } from "@/server/api/common/read-messaging-message.js"; diff --git a/packages/backend/src/remote/activitypub/resolver.ts b/packages/backend/src/remote/activitypub/resolver.ts index 18f88d538..62f3f2557 100644 --- a/packages/backend/src/remote/activitypub/resolver.ts +++ b/packages/backend/src/remote/activitypub/resolver.ts @@ -3,7 +3,8 @@ import { getJson } from "@/misc/fetch.js"; import type { ILocalUser } from "@/models/entities/user.js"; import { getInstanceActor } from "@/services/instance-actor.js"; import { fetchMeta } from "@/misc/fetch-meta.js"; -import { extractHost, isSelfHost } from "native-utils/built/index.js"; +import { extractHost } from "native-utils/built/index.js"; +import { isSelfHost } from "@/misc/native-utils.js"; import { signedGet } from "./request.js"; import type { IObject, ICollection, IOrderedCollection } from "./type.js"; import { isCollectionOrOrderedCollection, getApId } from "./type.js"; diff --git a/packages/backend/src/server/activitypub.ts b/packages/backend/src/server/activitypub.ts index e21040a3e..8e3160e51 100644 --- a/packages/backend/src/server/activitypub.ts +++ b/packages/backend/src/server/activitypub.ts @@ -9,7 +9,7 @@ import renderKey from "@/remote/activitypub/renderer/key.js"; import { renderPerson } from "@/remote/activitypub/renderer/person.js"; import renderEmoji from "@/remote/activitypub/renderer/emoji.js"; import { inbox as processInbox } from "@/queue/index.js"; -import { isSelfHost } from "native-utils/built/index.js"; +import { isSelfHost } from "@/misc/native-utils.js"; import { Notes, Users, diff --git a/packages/backend/src/server/index.ts b/packages/backend/src/server/index.ts index 4fcc6912c..14bfe74a3 100644 --- a/packages/backend/src/server/index.ts +++ b/packages/backend/src/server/index.ts @@ -20,7 +20,7 @@ import { fetchMeta } from "@/misc/fetch-meta.js"; import { genIdenticon } from "@/misc/gen-identicon.js"; import { createTemp } from "@/misc/create-temp.js"; import { stringToAcct } from "native-utils/built/index.js"; -import { envOption } from "@/config/index.js"; +import { envConfig } from "@/misc/native-utils.js"; import megalodon, { MegalodonInterface } from "megalodon"; import activityPub from "./activitypub.js"; import nodeinfo from "./nodeinfo.js"; @@ -57,7 +57,7 @@ if (!["production", "test"].includes(process.env.NODE_ENV || "")) { ); // Delay - if (envOption.slow) { + if (envConfig.slow) { app.use( slow({ delay: 3000, diff --git a/packages/backend/src/services/logger.ts b/packages/backend/src/services/logger.ts index d4be47aff..810baed29 100644 --- a/packages/backend/src/services/logger.ts +++ b/packages/backend/src/services/logger.ts @@ -2,7 +2,7 @@ import cluster from "node:cluster"; import chalk from "chalk"; import { default as convertColor } from "color-convert"; import { format as dateFormat } from "date-fns"; -import { envOption } from "@/config/index.js"; +import { envConfig } from "@/misc/native-utils.js"; import config from "@/config/index.js"; import * as SyslogPro from "syslog-pro"; @@ -56,7 +56,7 @@ export default class Logger { subDomains: Domain[] = [], store = true, ): void { - if (envOption.quiet) return; + if (envConfig.quiet) return; if (!this.store) store = false; if (level === "debug") store = false; @@ -111,7 +111,7 @@ export default class Logger { : null; let log = `${l} ${worker}\t[${domains.join(" ")}]\t${m}`; - if (envOption.withLogTime) log = `${chalk.gray(time)} ${log}`; + if (envConfig.withLogTime) log = `${chalk.gray(time)} ${log}`; console.log(important ? chalk.bold(log) : log); @@ -187,7 +187,7 @@ export default class Logger { important = false, ): void { // デバッグ用に使う(開発者に必要だが利用者に不要な情報) - if (process.env.NODE_ENV !== "production" || envOption.verbose) { + if (process.env.NODE_ENV !== "production" || envConfig.verbose) { this.log("debug", message, data, important); } }