From 49d0da3a6d54a65db7e11b1e9bb2e45fee228bdc Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 16 Aug 2024 00:22:24 +0800 Subject: [PATCH] refactor: remove lodash --- pnpm-lock.yaml | 6 --- src/server/cache/index.ts | 4 +- src/server/cronjob/index.ts | 4 +- src/server/model/auth.ts | 8 ++-- src/server/model/monitor/provider/openai.ts | 1 - src/server/model/serverStatus.ts | 1 - src/server/package.json | 2 - src/server/trpc/routers/feed/index.ts | 1 - src/server/trpc/routers/feed/integration.ts | 50 ++++++++++----------- src/server/trpc/trpc.ts | 4 +- src/server/utils/common.ts | 1 - src/server/utils/env.ts | 4 +- src/server/utils/prisma.ts | 6 +-- 13 files changed, 40 insertions(+), 52 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c77d2ec..98064b7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -501,9 +501,6 @@ importers: jsonwebtoken: specifier: ^9.0.2 version: 9.0.2 - lodash: - specifier: ^4.17.21 - version: 4.17.21 lodash-es: specifier: ^4.17.21 version: 4.17.21 @@ -592,9 +589,6 @@ importers: '@types/jsonwebtoken': specifier: ^9.0.5 version: 9.0.5 - '@types/lodash': - specifier: ^4.14.198 - version: 4.14.198 '@types/lodash-es': specifier: ^4.17.12 version: 4.17.12 diff --git a/src/server/cache/index.ts b/src/server/cache/index.ts index 7bd6362..abef3aa 100644 --- a/src/server/cache/index.ts +++ b/src/server/cache/index.ts @@ -1,5 +1,5 @@ import { caching, MemoryCache } from 'cache-manager'; -import _ from 'lodash'; +import { uniqueId } from 'lodash-es'; let _cacheManager: MemoryCache; export async function getCacheManager() { @@ -20,7 +20,7 @@ export async function getCacheManager() { export function buildQueryWithCache( fetchFn: (...args: Args) => Promise ) { - const id = _.uniqueId('cache-query'); + const id = uniqueId('cache-query'); const get = async (...args: Args): Promise => { const key = [id, ...args.map((a) => JSON.stringify(a))].join('|'); diff --git a/src/server/cronjob/index.ts b/src/server/cronjob/index.ts index 52a8149..ddfa3d3 100644 --- a/src/server/cronjob/index.ts +++ b/src/server/cronjob/index.ts @@ -6,9 +6,9 @@ import { FeedChannelNotifyFrequency, Prisma } from '@prisma/client'; import { env } from '../utils/env.js'; import { sendNotification } from '../model/notification/index.js'; import { token } from '../model/notification/token/index.js'; -import _ from 'lodash'; import pMap from 'p-map'; import { sendFeedEventsNotify } from '../model/feed/event.js'; +import { get } from 'lodash-es'; type WebsiteEventCountSqlReturn = { workspace_id: string; @@ -368,7 +368,7 @@ async function dailyHTTPCertCheckNotify() { continue; } - const content = `[${m.name}][${_.get(m.payload, 'url')}] Certificate will be expired in ${daysRemaining} days`; + const content = `[${m.name}][${get(m.payload, 'url')}] Certificate will be expired in ${daysRemaining} days`; try { await sendNotification(n, content, [token.text(content)]).catch( diff --git a/src/server/model/auth.ts b/src/server/model/auth.ts index 2e9c4a1..23e26dd 100644 --- a/src/server/model/auth.ts +++ b/src/server/model/auth.ts @@ -17,9 +17,9 @@ import { createTransport } from 'nodemailer'; import { Theme } from '@auth/core/types'; import { generateSMTPHTML } from '../utils/smtp.js'; import { SYSTEM_ROLES } from '@tianji/shared'; -import _ from 'lodash'; import { IncomingMessage } from 'http'; import { type Session } from '@auth/express'; +import { compact, set } from 'lodash-es'; export interface UserAuthPayload { id: string; @@ -31,7 +31,7 @@ export const authConfig: Omit = { debug: env.isProd ? false : true, basePath: '/api/auth', trustHost: true, - providers: _.compact([ + providers: compact([ Credentials({ id: 'account', name: 'Account', @@ -103,8 +103,8 @@ export const authConfig: Omit = { }; }, session({ session, token, user }) { - _.set(session, ['user', 'id'], token.sub); - _.set(session, ['user', 'role'], token.role); + set(session, ['user', 'id'], token.sub); + set(session, ['user', 'role'], token.role); return session; }, diff --git a/src/server/model/monitor/provider/openai.ts b/src/server/model/monitor/provider/openai.ts index e0ba7a7..28bedaa 100644 --- a/src/server/model/monitor/provider/openai.ts +++ b/src/server/model/monitor/provider/openai.ts @@ -2,7 +2,6 @@ import { z } from 'zod'; import { MonitorProvider } from './type.js'; import axios from 'axios'; import { saveMonitorStatus } from './_utils.js'; -import _ from 'lodash'; const openaiCreditGrantsSchema = z.object({ object: z.string(), diff --git a/src/server/model/serverStatus.ts b/src/server/model/serverStatus.ts index dc59457..ce3f733 100644 --- a/src/server/model/serverStatus.ts +++ b/src/server/model/serverStatus.ts @@ -1,6 +1,5 @@ import { ServerStatusInfo } from '../../types/index.js'; import { createSubscribeInitializer, subscribeEventBus } from '../ws/shared.js'; -import _ from 'lodash'; import { isServerOnline } from '@tianji/shared'; const serverMap: Record< diff --git a/src/server/package.json b/src/server/package.json index 0159f3e..46a023f 100644 --- a/src/server/package.json +++ b/src/server/package.json @@ -50,7 +50,6 @@ "is-localhost-ip": "^2.0.0", "isolated-vm": "^4.7.2", "jsonwebtoken": "^9.0.2", - "lodash": "^4.17.21", "lodash-es": "^4.17.21", "maxmind": "^4.3.18", "md5": "^2.3.0", @@ -82,7 +81,6 @@ "@types/express": "^4.17.17", "@types/fs-extra": "^11.0.3", "@types/jsonwebtoken": "^9.0.5", - "@types/lodash": "^4.14.198", "@types/lodash-es": "^4.17.12", "@types/md5": "^2.3.5", "@types/morgan": "^1.9.5", diff --git a/src/server/trpc/routers/feed/index.ts b/src/server/trpc/routers/feed/index.ts index 5924870..6a9060d 100644 --- a/src/server/trpc/routers/feed/index.ts +++ b/src/server/trpc/routers/feed/index.ts @@ -13,7 +13,6 @@ import { FeedEventModelSchema, } from '../../../prisma/zod/index.js'; import { prisma } from '../../../model/_client.js'; -import _ from 'lodash'; import { buildFeedPublicOpenapi, feedIntegrationRouter, diff --git a/src/server/trpc/routers/feed/integration.ts b/src/server/trpc/routers/feed/integration.ts index 54c45a8..bf08dd2 100644 --- a/src/server/trpc/routers/feed/integration.ts +++ b/src/server/trpc/routers/feed/integration.ts @@ -1,12 +1,12 @@ import { z } from 'zod'; import { OpenApiMetaInfo, publicProcedure, router } from '../../trpc.js'; import { prisma } from '../../../model/_client.js'; -import _ from 'lodash'; import { OpenApiMeta } from 'trpc-openapi'; import { OPENAPI_TAG } from '../../../utils/const.js'; import { createFeedEvent } from '../../../model/feed/event.js'; import { tencentCloudAlarmSchema } from '../../../model/_schema/feed.js'; import { logger } from '../../../utils/logger.js'; +import { get, map } from 'lodash-es'; export const feedIntegrationRouter = router({ github: publicProcedure @@ -45,17 +45,17 @@ export const feedIntegrationRouter = router({ } if (eventType === 'push') { - const pusherName = _.get(data, 'pusher.name'); - const pusherEmail = _.get(data, 'pusher.email'); - const commits = _.map(_.get(data, 'commits') as any[], (val) => + const pusherName = get(data, 'pusher.name'); + const pusherEmail = get(data, 'pusher.email'); + const commits = map(get(data, 'commits') as any[], (val) => String(val.id).substring(0, 9) ).join(', '); - const fullName = _.get(data, 'repository.full_name'); - const repoUrl = _.get(data, 'repository.html_url'); - const ref = String(_.get(data, 'ref')); - const senderId = String(_.get(data, 'sender.id')); - const senderName = String(_.get(data, 'sender.login')); - const url = String(_.get(data, 'compare')); + const fullName = get(data, 'repository.full_name'); + const repoUrl = get(data, 'repository.html_url'); + const ref = String(get(data, 'ref')); + const senderId = String(get(data, 'sender.id')); + const senderName = String(get(data, 'sender.login')); + const url = String(get(data, 'compare')); await createFeedEvent(workspaceId, { channelId: channelId, @@ -71,13 +71,13 @@ export const feedIntegrationRouter = router({ return 'ok'; } else if (eventType === 'star') { - const starCount = _.get(data, 'repository.stargazers_count'); - const fullName = _.get(data, 'repository.full_name'); - const repoUrl = _.get(data, 'repository.html_url'); - const senderId = String(_.get(data, 'sender.id')); - const senderName = String(_.get(data, 'sender.login')); - const senderUrl = String(_.get(data, 'sender.html_url')); - const url = String(_.get(data, 'compare')); + const starCount = get(data, 'repository.stargazers_count'); + const fullName = get(data, 'repository.full_name'); + const repoUrl = get(data, 'repository.html_url'); + const senderId = String(get(data, 'sender.id')); + const senderName = String(get(data, 'sender.login')); + const senderUrl = String(get(data, 'sender.html_url')); + const url = String(get(data, 'compare')); await createFeedEvent(workspaceId, { channelId: channelId, @@ -93,14 +93,14 @@ export const feedIntegrationRouter = router({ return 'ok'; } else if (eventType === 'issues') { - const action = _.get(data, 'action') as 'opened' | 'closed'; - const starCount = _.get(data, 'repository.stargazers_count'); - const fullName = _.get(data, 'repository.full_name'); - const repoUrl = _.get(data, 'repository.html_url'); - const senderId = String(_.get(data, 'sender.id')); - const senderName = String(_.get(data, 'sender.login')); - const url = String(_.get(data, 'issue.url')); - const title = String(_.get(data, 'issue.title')); + const action = get(data, 'action') as 'opened' | 'closed'; + const starCount = get(data, 'repository.stargazers_count'); + const fullName = get(data, 'repository.full_name'); + const repoUrl = get(data, 'repository.html_url'); + const senderId = String(get(data, 'sender.id')); + const senderName = String(get(data, 'sender.login')); + const url = String(get(data, 'issue.url')); + const title = String(get(data, 'issue.title')); let eventName = eventType; let eventContent = ''; diff --git a/src/server/trpc/trpc.ts b/src/server/trpc/trpc.ts index 519e115..f9e1b07 100644 --- a/src/server/trpc/trpc.ts +++ b/src/server/trpc/trpc.ts @@ -1,5 +1,4 @@ import { initTRPC, inferAsyncReturnType, TRPCError } from '@trpc/server'; -import _ from 'lodash'; import { z } from 'zod'; import { jwtVerify } from '../middleware/auth.js'; import { getWorkspaceUser } from '../model/workspace.js'; @@ -8,6 +7,7 @@ import type { Request } from 'express'; import { OpenApiMeta } from 'trpc-openapi'; import { getSession } from '@auth/express'; import { authConfig } from '../model/auth.js'; +import { get } from 'lodash-es'; export async function createContext({ req }: { req: Request }) { const authorization = req.headers['authorization'] ?? ''; @@ -97,7 +97,7 @@ function createWorkspacePermissionMiddleware(roles: ROLES[] = []) { return isUser.unstable_pipe(async (opts) => { const { ctx, input } = opts; - const workspaceId = _.get(input, 'workspaceId', ''); + const workspaceId = get(input, 'workspaceId', ''); if (!workspaceId) { throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', diff --git a/src/server/utils/common.ts b/src/server/utils/common.ts index 9570a8b..7ae76e3 100644 --- a/src/server/utils/common.ts +++ b/src/server/utils/common.ts @@ -5,7 +5,6 @@ import { DynamicDataType } from './types.js'; import dayjs from 'dayjs'; import minMax from 'dayjs/plugin/minMax.js'; import jwt from 'jsonwebtoken'; -import _ from 'lodash'; import { getWorkspaceWebsiteDateRange } from '../model/workspace.js'; import { isCuid } from '@paralleldrive/cuid2'; import { getMinimumUnit } from '@tianji/shared'; diff --git a/src/server/utils/env.ts b/src/server/utils/env.ts index 6aed520..6fbd23b 100644 --- a/src/server/utils/env.ts +++ b/src/server/utils/env.ts @@ -1,6 +1,6 @@ import { v1 as uuid } from 'uuid'; import md5 from 'md5'; -import _ from 'lodash'; +import { compact } from 'lodash-es'; const jwtSecret = !process.env.JWT_SECRET || @@ -14,7 +14,7 @@ export const env = { jwtSecret, port: Number(process.env.PORT || 12345), auth: { - provider: _.compact([ + provider: compact([ !!process.env.EMAIL_SERVER && 'email', !!process.env.AUTH_GITHUB_ID && 'github', !!process.env.AUTH_GOOGLE_ID && 'google', diff --git a/src/server/utils/prisma.ts b/src/server/utils/prisma.ts index 02efc1f..174434c 100644 --- a/src/server/utils/prisma.ts +++ b/src/server/utils/prisma.ts @@ -1,10 +1,10 @@ import { Prisma } from '@prisma/client'; import dayjs from 'dayjs'; -import _ from 'lodash'; import { loadWebsite } from '../model/website.js'; import { maxDate } from './common.js'; import { FILTER_COLUMNS, OPERATORS, SESSION_COLUMNS } from './const.js'; import { loadTelemetry } from '../model/telemetry.js'; +import { get } from 'lodash-es'; const POSTGRESQL_DATE_FORMATS = { minute: 'YYYY-MM-DD HH24:MI:00', @@ -136,7 +136,7 @@ export function getWebsiteFilterQuery( const query = Object.keys(filters).reduce((arr, name) => { const value: any = filters[name as keyof WebsiteQueryFilters]; const operator = value?.filter ?? OPERATORS.equals; - const column = _.get(FILTER_COLUMNS, name, options?.columns?.[name]); + const column = get(FILTER_COLUMNS, name, options?.columns?.[name]); // TODO @@ -163,7 +163,7 @@ export function getTelemetryFilterQuery( const query = Object.keys(filters).reduce((arr, name) => { const value: any = filters[name as keyof BaseQueryFilters]; const operator = value?.filter ?? OPERATORS.equals; - const column = _.get(FILTER_COLUMNS, name, options?.columns?.[name]); + const column = get(FILTER_COLUMNS, name, options?.columns?.[name]); // TODO