Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Aug 16, 2024
1 parent 1bb4f6a commit 817b81a
Show file tree
Hide file tree
Showing 178 changed files with 507 additions and 410 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { factory } from '@innei/prettier'
export default {
...factory({
tailwindcss: false,
importSort: false,
importSort: true,
}),
importOrderParserPlugins: ['typescript', 'jsx', 'decorators-legacy'],
}
1 change: 0 additions & 1 deletion apps/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"dev": "npm run start",
"repl": "npm run start -- --entryFile repl",
"bundle": "rimraf out && npm run build && cd dist/src && npx ncc build main.js -o ../../out --minify -s && cd ../.. && chmod +x out/index.js && node scripts/after-bundle.js",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\" \"packages/**/*.ts\"",
"start": "cross-env NODE_ENV=development nest start -w --path tsconfig.json -- ",
"start:debug": "cross-env NODE_ENV=development nest start --debug --watch",
"start:cluster": "cross-env NODE_ENV=development nest start --watch -- --cluster --cluster_workers 2",
Expand Down
8 changes: 4 additions & 4 deletions apps/core/src/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { seconds } from '@nestjs/throttler'
import { program } from 'commander'
import { load as yamlLoad } from 'js-yaml'

import { machineIdSync } from 'node-machine-id'
import { isDebugMode, isDev } from './global/env.global'
import type { AxiosRequestConfig } from 'axios'

import { seconds } from '@nestjs/throttler'

import { isDebugMode, isDev } from './global/env.global'
import { parseBooleanishValue } from './utils/tool.util'
import type { AxiosRequestConfig } from 'axios'

const { PORT: ENV_PORT, ALLOWED_ORIGINS, MX_ENCRYPT_KEY } = process.env

Expand Down
14 changes: 7 additions & 7 deletions apps/core/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { LoggerModule } from '@innei/pretty-logger-nestjs'
import type {
DynamicModule,
MiddlewareConsumer,
NestModule,
Type,
} from '@nestjs/common'

import { LoggerModule } from '@innei/pretty-logger-nestjs'
import { Module } from '@nestjs/common'
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core'

Expand Down Expand Up @@ -60,12 +66,6 @@ import { DatabaseModule } from './processors/database/database.module'
import { GatewayModule } from './processors/gateway/gateway.module'
import { HelperModule } from './processors/helper/helper.module'
import { RedisModule } from './processors/redis/redis.module'
import type {
DynamicModule,
MiddlewareConsumer,
NestModule,
Type,
} from '@nestjs/common'

@Module({
imports: [
Expand Down
8 changes: 4 additions & 4 deletions apps/core/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import cluster from 'node:cluster'
import { performance } from 'node:perf_hooks'
import wcmatch from 'wildcard-match'
import type { LogLevel } from '@nestjs/common'
import type { NestFastifyApplication } from '@nestjs/platform-fastify'

import { Logger } from '@innei/pretty-logger-nestjs'
import { NestFactory } from '@nestjs/core'

import wcmatch from 'wildcard-match'

import { CROSS_DOMAIN, DEBUG_MODE, PORT } from './app.config'
import { AppModule } from './app.module'
import { fastifyApp } from './common/adapters/fastify.adapter'
Expand All @@ -15,8 +17,6 @@ import { ExtendedValidationPipe } from './common/pipes/validation.pipe'
import { logger } from './global/consola.global'
import { isMainProcess, isTest } from './global/env.global'
import { checkInit } from './utils/check-init.util'
import type { LogLevel } from '@nestjs/common'
import type { NestFastifyApplication } from '@nestjs/platform-fastify'

const Origin: false | string[] = Array.isArray(CROSS_DOMAIN.allowedOrigins)
? [...CROSS_DOMAIN.allowedOrigins, '*.shizuri.net', '22333322.xyz']
Expand Down
5 changes: 3 additions & 2 deletions apps/core/src/common/adapters/fastify.adapter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { FastifyRequest } from 'fastify'

import fastifyCookie from '@fastify/cookie'
import FastifyMultipart from '@fastify/multipart'
import { Logger } from '@nestjs/common'
import { FastifyAdapter } from '@nestjs/platform-fastify'

import { getIp } from '~/utils'
import type { FastifyRequest } from 'fastify'

const app: FastifyAdapter = new FastifyAdapter({
trustProxy: true,
Expand Down Expand Up @@ -38,7 +39,7 @@ app.getInstance().addHook('onRequest', (request, reply, done) => {
logWarn('PHP 是世界上最好的语言!!!!!', request, 'GodPHP')

return reply.code(418).send()
} else if (/\/(adminer|admin|wp-login|phpmyadmin|\.env)$/gi.test(url)) {
} else if (/\/(?:adminer|admin|wp-login|phpmyadmin|\.env)$/i.test(url)) {
const isMxSpaceClient = ua?.match('mx-space')
reply.raw.statusMessage = 'Hey, What the fuck are you doing!'
reply.raw.statusCode = isMxSpaceClient ? 666 : 200
Expand Down
3 changes: 2 additions & 1 deletion apps/core/src/common/adapters/socket.adapter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Server } from 'socket.io'

import { IoAdapter } from '@nestjs/platform-socket.io'
import { createAdapter } from '@socket.io/redis-adapter'

import { redisSubPub } from '~/utils/redis-subpub.util'
import type { Server } from 'socket.io'

export const RedisIoAdapterKey = 'mx-core-socket'

Expand Down
3 changes: 2 additions & 1 deletion apps/core/src/common/contexts/request.context.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* eslint-disable dot-notation */
// @reference https://github.com/ever-co/ever-gauzy/blob/d36b4f40b1446f3c33d02e0ba00b53a83109d950/packages/core/src/core/context/request-context.ts
import * as cls from 'cls-hooked'
import { UnauthorizedException } from '@nestjs/common'
import type { UserDocument } from '~/modules/user/user.model'
import type { IncomingMessage, ServerResponse } from 'node:http'

import { UnauthorizedException } from '@nestjs/common'

type Nullable<T> = T | null
export class RequestContext {
readonly id: number
Expand Down
3 changes: 2 additions & 1 deletion apps/core/src/common/decorators/api-controller.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { ControllerOptions } from '@nestjs/common'

import { Controller } from '@nestjs/common'

import { API_VERSION } from '~/app.config'
import { isDev } from '~/global/env.global'
import type { ControllerOptions } from '@nestjs/common'

export const apiRoutePrefix = isDev ? '' : `/api/v${API_VERSION}`
export const ApiController: (
Expand Down
2 changes: 1 addition & 1 deletion apps/core/src/common/decorators/auth.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UseGuards, applyDecorators } from '@nestjs/common'
import { applyDecorators, UseGuards } from '@nestjs/common'

import { AuthGuard } from '../guards/auth.guard'

Expand Down
3 changes: 2 additions & 1 deletion apps/core/src/common/decorators/cookie.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createParamDecorator } from '@nestjs/common'
import type { ExecutionContext } from '@nestjs/common'
import type { FastifyRequest } from 'fastify'

import { createParamDecorator } from '@nestjs/common'

export const Cookies = createParamDecorator(
(data: string, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest<FastifyRequest>()
Expand Down
3 changes: 2 additions & 1 deletion apps/core/src/common/decorators/current-user.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ExecutionContext } from '@nestjs/common'

import { createParamDecorator } from '@nestjs/common'

import { getNestExecutionContextRequest } from '~/transformers/get-req.transformer'
import type { ExecutionContext } from '@nestjs/common'

export const CurrentUser = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
Expand Down
6 changes: 4 additions & 2 deletions apps/core/src/common/decorators/demo.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { UseGuards, applyDecorators } from '@nestjs/common'
import { banInDemo } from '~/utils'
import type { CanActivate } from '@nestjs/common'
import type { Observable } from 'rxjs'

import { applyDecorators, UseGuards } from '@nestjs/common'

import { banInDemo } from '~/utils'

class DemoGuard implements CanActivate {
canActivate(): boolean | Promise<boolean> | Observable<boolean> {
banInDemo()
Expand Down
3 changes: 2 additions & 1 deletion apps/core/src/common/decorators/http.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { IdempotenceOption } from '../interceptors/idempotence.interceptor'

import { SetMetadata } from '@nestjs/common'

import {
HTTP_IDEMPOTENCE_OPTIONS,
HTTP_RES_TRANSFORM_PAGINATE,
} from '~/constants/meta.constant'
import * as SYSTEM from '~/constants/system.constant'
import type { IdempotenceOption } from '../interceptors/idempotence.interceptor'

/**
* @description 分页转换
Expand Down
6 changes: 4 additions & 2 deletions apps/core/src/common/decorators/ip.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
* @FilePath: /mx-server/src/core/decorators/ip.decorator.ts
* @Coding with Love
*/
import { createParamDecorator } from '@nestjs/common'
import { getIp } from '~/utils/ip.util'
import type { ExecutionContext } from '@nestjs/common'
import type { FastifyRequest } from 'fastify'

import { createParamDecorator } from '@nestjs/common'

import { getIp } from '~/utils/ip.util'

export type IpRecord = {
ip: string
agent: string
Expand Down
3 changes: 2 additions & 1 deletion apps/core/src/common/decorators/role.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { ExecutionContext } from '@nestjs/common'

import { createParamDecorator } from '@nestjs/common'

import { isTest } from '~/global/env.global'
import { getNestExecutionContextRequest } from '~/transformers/get-req.transformer'
import type { ExecutionContext } from '@nestjs/common'

export const IsGuest = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
Expand Down
6 changes: 3 additions & 3 deletions apps/core/src/common/filters/any-exception.filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { resolve } from 'node:path'
import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common'
import type { FastifyReply, FastifyRequest } from 'fastify'
import type { WriteStream } from 'node:fs'

import {
Catch,
Expand All @@ -21,9 +24,6 @@ import { EventManagerService } from '~/processors/helper/helper.event.service'
import { getIp } from '../../utils/ip.util'
import { BizException } from '../exceptions/biz.exception'
import { LoggingInterceptor } from '../interceptors/logging.interceptor'
import type { WriteStream } from 'node:fs'
import type { FastifyReply, FastifyRequest } from 'fastify'
import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common'

type myError = {
readonly status: number
Expand Down
6 changes: 3 additions & 3 deletions apps/core/src/common/guards/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { isJWT } from 'class-validator'
import type { CanActivate, ExecutionContext } from '@nestjs/common'
import type { UserModel } from '~/modules/user/user.model'
import type { FastifyBizRequest } from '~/transformers/get-req.transformer'

import { Injectable, UnauthorizedException } from '@nestjs/common'

import { AuthService } from '~/modules/auth/auth.service'
import { ConfigsService } from '~/modules/configs/configs.service'
import { UserService } from '~/modules/user/user.service'
import { getNestExecutionContextRequest } from '~/transformers/get-req.transformer'
import type { FastifyBizRequest } from '~/transformers/get-req.transformer'
import type { UserModel } from '~/modules/user/user.model'
import type { CanActivate, ExecutionContext } from '@nestjs/common'

/**
* JWT auth guard
Expand Down
3 changes: 2 additions & 1 deletion apps/core/src/common/guards/roles.guard.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { CanActivate, ExecutionContext } from '@nestjs/common'

import { Injectable } from '@nestjs/common'

import { AuthService } from '~/modules/auth/auth.service'
Expand All @@ -6,7 +8,6 @@ import { UserService } from '~/modules/user/user.service'
import { getNestExecutionContextRequest } from '~/transformers/get-req.transformer'

import { AuthGuard } from './auth.guard'
import type { CanActivate, ExecutionContext } from '@nestjs/common'

/**
* 区分游客和主人的守卫
Expand Down
9 changes: 5 additions & 4 deletions apps/core/src/common/guards/spider.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
* @author Innei <https://innei.in>
*/

import type { CanActivate, ExecutionContext } from '@nestjs/common'
import type { Observable } from 'rxjs'

import { ForbiddenException, Injectable } from '@nestjs/common'

import { isDev } from '~/global/env.global'
import { getNestExecutionContextRequest } from '~/transformers/get-req.transformer'
import type { Observable } from 'rxjs'
import type { CanActivate, ExecutionContext } from '@nestjs/common'

@Injectable()
export class SpiderGuard implements CanActivate {
Expand All @@ -24,8 +25,8 @@ export class SpiderGuard implements CanActivate {
const headers = request.headers
const ua: string = headers['user-agent'] || ''
const isSpiderUA =
!!/(scrapy|httpclient|axios|python|requests)/i.test(ua) &&
!/(mx-space|rss|google|baidu|bing)/gi.test(ua)
!!/scrapy|httpclient|axios|python|requests/i.test(ua) &&
!/mx-space|rss|google|baidu|bing/i.test(ua)
if (ua && !isSpiderUA) {
return true
}
Expand Down
5 changes: 3 additions & 2 deletions apps/core/src/common/guards/throttler.guard.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { ExecutionContext } from '@nestjs/common'
import type { FastifyBizRequest } from '~/transformers/get-req.transformer'

import { Injectable } from '@nestjs/common'
import { ThrottlerGuard } from '@nestjs/throttler'

import { getNestExecutionContextRequest } from '~/transformers/get-req.transformer'
import { getIp } from '~/utils'
import type { FastifyBizRequest } from '~/transformers/get-req.transformer'
import type { ExecutionContext } from '@nestjs/common'

@Injectable()
export class ExtendThrottlerGuard extends ThrottlerGuard {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { RequestMethod } from '@nestjs/common'
import type {
CallHandler,
ExecutionContext,
NestInterceptor,
} from '@nestjs/common'
import type { FastifyReply } from 'fastify'

import { RequestMethod } from '@nestjs/common'

export class AllowAllCorsInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler<any>) {
const handle = next.handle()
Expand Down
12 changes: 6 additions & 6 deletions apps/core/src/common/interceptors/analyze.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
import { URL } from 'node:url'
import { isbot } from 'isbot'
import UAParser from 'ua-parser-js'
import type {
CallHandler,
ExecutionContext,
NestInterceptor,
} from '@nestjs/common'
import type { Observable } from 'rxjs'

import { Inject, Injectable } from '@nestjs/common'
import { Reflector } from '@nestjs/core'
Expand All @@ -23,12 +29,6 @@ import { InjectModel } from '~/transformers/model.transformer'
import { scheduleManager } from '~/utils'
import { getIp } from '~/utils/ip.util'
import { getRedisKey } from '~/utils/redis.util'
import type { Observable } from 'rxjs'
import type {
CallHandler,
ExecutionContext,
NestInterceptor,
} from '@nestjs/common'

@Injectable()
export class AnalyzeInterceptor implements NestInterceptor {
Expand Down
14 changes: 7 additions & 7 deletions apps/core/src/common/interceptors/cache.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
* @author Innei <https://innei.in>
*/
import { of, tap } from 'rxjs'
import type {
CallHandler,
ExecutionContext,
NestInterceptor,
} from '@nestjs/common'
import type { FastifyReply } from 'fastify'
import type { Observable } from 'rxjs'

import { Inject, Injectable, Logger, RequestMethod } from '@nestjs/common'
import { HttpAdapterHost, Reflector } from '@nestjs/core'
Expand All @@ -17,13 +24,6 @@ import * as SYSTEM from '~/constants/system.constant'
import { CacheService } from '~/processors/redis/cache.service'
import { getNestExecutionContextRequest } from '~/transformers/get-req.transformer'
import { hashString } from '~/utils'
import type { Observable } from 'rxjs'
import type { FastifyReply } from 'fastify'
import type {
CallHandler,
ExecutionContext,
NestInterceptor,
} from '@nestjs/common'

/**
* @class HttpCacheInterceptor
Expand Down
6 changes: 4 additions & 2 deletions apps/core/src/common/interceptors/db-query.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import qs from 'qs'
import { Injectable } from '@nestjs/common'
import { getNestExecutionContextRequest } from '~/transformers/get-req.transformer'
import type {
CallHandler,
ExecutionContext,
NestInterceptor,
} from '@nestjs/common'
import type { Observable } from 'rxjs'

import { Injectable } from '@nestjs/common'

import { getNestExecutionContextRequest } from '~/transformers/get-req.transformer'

/** 此拦截器用于转换 req.query.query -> js object,用于直接数据库查询,需要鉴权 */
@Injectable()
export class DbQueryInterceptor implements NestInterceptor {
Expand Down
Loading

0 comments on commit 817b81a

Please sign in to comment.