Skip to content

Commit

Permalink
refactor: only require lib when needed
Browse files Browse the repository at this point in the history
仅在需要时导入第三方依赖,避免某一个依赖出现兼容性问题而无法导入其他依赖。
  • Loading branch information
imaegoo committed May 14, 2024
1 parent ed68d51 commit 113e678
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 96 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ yarn.lock
src/server/pkg/dist/*
src/server/pkg/patches/*
src/server/pkg/web.config
pnpm-lock.yaml
pnpm-lock.yaml
Spacefile
11 changes: 7 additions & 4 deletions src/server/function/twikoo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
const { version: VERSION } = require('./package.json')
const tcb = require('@cloudbase/node-sdk') // 云开发 SDK
const {
$,
md5,
xml2js
getCheerio,
getDomPurify,
getMd5,
getXml2js
} = require('./utils/lib')
const {
getFuncVersion,
Expand Down Expand Up @@ -43,14 +44,16 @@ const { postCheckSpam } = require('./utils/spam')
const { sendNotice, emailTest } = require('./utils/notify')
const { uploadImage } = require('./utils/image')
const logger = require('./utils/logger')
const { getDomPurify } = require('./utils/dom')

// 云函数 SDK / tencent cloudbase sdk
const app = tcb.init({ env: tcb.SYMBOL_CURRENT_ENV })
const auth = app.auth()
const db = app.database()
const _ = db.command
const $ = getCheerio()
const DOMPurify = getDomPurify()
const md5 = getMd5()
const xml2js = getXml2js()

// 常量 / constants
const { RES_CODE, MAX_REQUEST_TIMES } = require('./utils/constants')
Expand Down
5 changes: 2 additions & 3 deletions src/server/function/twikoo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twikoo-func",
"version": "1.6.33",
"version": "1.6.32",
"description": "A simple comment system.",
"author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
"license": "MIT",
Expand All @@ -27,7 +27,6 @@
"nodemailer": "^6.4.17",
"pushoo": "latest",
"tencentcloud-sdk-nodejs": "^4.0.65",
"xml2js": "^0.6.0",
"xss": "^1.0.15"
"xml2js": "^0.6.0"
}
}
7 changes: 0 additions & 7 deletions src/server/function/twikoo/utils/cloudflare.js

This file was deleted.

32 changes: 0 additions & 32 deletions src/server/function/twikoo/utils/dom.js

This file was deleted.

4 changes: 3 additions & 1 deletion src/server/function/twikoo/utils/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const os = require('os')
const path = require('path')
const { isUrl } = require('.')
const { RES_CODE } = require('./constants')
const { axios, FormData } = require('./lib')
const { getAxios, getFormData } = require('./lib')
const axios = getAxios()
const FormData = getFormData()
const logger = require('./logger')

const fn = {
Expand Down
5 changes: 3 additions & 2 deletions src/server/function/twikoo/utils/import.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { getRelativeUrl, normalizeMail } = require('.')
const { marked, md5 } = require('./lib')
const { getDomPurify } = require('./dom')
const { getMarked, getDomPurify, getMd5 } = require('./lib')
const marked = getMarked()
const md5 = getMd5()

const fn = {
// 兼容 Leancloud 两种 JSON 导出格式
Expand Down
18 changes: 16 additions & 2 deletions src/server/function/twikoo/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
const { URL } = require('url')
const { axios, FormData, bowser, md5 } = require('./lib')
const {
getAxios,
getFormData,
getBowser,
getIpToRegion,
getMd5
} = require('./lib')
const axios = getAxios()
const FormData = getFormData()
const bowser = getBowser()
const ipToRegion = getIpToRegion()
const md5 = getMd5()
const { RES_CODE } = require('./constants')
const logger = require('./logger')

let ipRegionSearcher

// IP 属地查询
function getIpRegionSearcher () {
return ipRegionSearcher ?? (ipRegionSearcher = require('@imaegoo/node-ip2region').create())
if (!ipRegionSearcher) {
ipRegionSearcher = ipToRegion.create() // 初始化 IP 属地
}
return ipRegionSearcher
}

const fn = {
Expand Down
81 changes: 60 additions & 21 deletions src/server/function/twikoo/utils/lib.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,62 @@
const $ = require('cheerio') // jQuery 服务器版
const { AkismetClient } = require('akismet-api') // 反垃圾 API
const CryptoJS = require('crypto-js') // 编解码
const FormData = require('form-data') // 图片上传
const axios = require('axios') // 发送 REST 请求
const bowser = require('bowser') // UserAgent 格式化
const marked = require('marked') // Markdown 解析
const md5 = require('blueimp-md5') // MD5 加解密
const pushoo = require('pushoo').default // 即时消息通知
const xml2js = require('xml2js') // XML 解析

module.exports = {
$,
AkismetClient,
CryptoJS,
FormData,
axios,
bowser,
marked,
md5,
pushoo,
xml2js
getCheerio () {
const $ = require('cheerio') // jQuery 服务器版
return $
},
getAkismetClient () {
const { AkismetClient } = require('akismet-api') // 反垃圾 API
return AkismetClient
},
getCryptoJS () {
const CryptoJS = require('crypto-js') // 编解码
return CryptoJS
},
getFormData () {
const FormData = require('form-data') // 图片上传
return FormData
},
getAxios () {
const axios = require('axios') // 发送 REST 请求
return axios
},
getBowser () {
const bowser = require('bowser') // UserAgent 格式化
return bowser
},
getDomPurify () {
// 初始化反 XSS
const { JSDOM } = require('jsdom') // document.window 服务器版
const createDOMPurify = require('dompurify') // 反 XSS
const window = new JSDOM('').window
const DOMPurify = createDOMPurify(window)
return DOMPurify
},
getIpToRegion () {
const ipToRegion = require('@imaegoo/node-ip2region') // IP 属地查询
return ipToRegion
},
getMarked () {
const marked = require('marked') // Markdown 解析
return marked
},
getMd5 () {
const md5 = require('blueimp-md5') // MD5 加解密
return md5
},
getNodemailer () {
const nodemailer = require('nodemailer') // 发送邮件
return nodemailer
},
getPushoo () {
const pushoo = require('pushoo').default // 即时消息通知
return pushoo
},
getTencentcloud () {
const tencentcloud = require('tencentcloud-sdk-nodejs') // 腾讯云 API NODEJS SDK
return tencentcloud
},
getXml2js () {
const xml2js = require('xml2js') // XML 解析
return xml2js
}
}
9 changes: 6 additions & 3 deletions src/server/function/twikoo/utils/notify.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const { equalsMail, getAvatar } = require('.')
const {
$,
nodemailer,
pushoo
getCheerio,
getNodemailer,
getPushoo
} = require('./lib')
const $ = getCheerio()
const nodemailer = getNodemailer()
const pushoo = getPushoo()
const { RES_CODE } = require('./constants')
const logger = require('./logger')

Expand Down
22 changes: 14 additions & 8 deletions src/server/function/twikoo/utils/spam.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
const {
AkismetClient,
CryptoJS,
getAkismetClient,
getCryptoJS,
getTencentcloud
} = require('./lib')
const AkismetClient = getAkismetClient()
const CryptoJS = getCryptoJS()

const logger = require('./logger')

let tencentcloud

function getTencentCloud () {
if (tencentcloud) return tencentcloud
try {
tencentcloud = require('tencentcloud-sdk-nodejs') // 腾讯云 API NODEJS SDK
} catch (e) {
logger.log('加载 "tencentcloud-sdk-nodejs" 失败', e)
if (!tencentcloud) {
try {
tencentcloud = getTencentcloud() // 腾讯云 API NODEJS SDK
} catch (e) {
logger.warn('加载 "tencentcloud-sdk-nodejs" 失败', e)
}
}
return tencentcloud
}
Expand All @@ -25,7 +31,7 @@ const fn = {
isSpam = true
} else if (config.QCLOUD_SECRET_ID && config.QCLOUD_SECRET_KEY) {
// 腾讯云内容安全
const client = new getTencentCloud().tms.v20200713.Client({
const client = new (getTencentCloud().tms.v20200713.Client)({
credential: { secretId: config.QCLOUD_SECRET_ID, secretKey: config.QCLOUD_SECRET_KEY },
region: 'ap-shanghai',
profile: { httpProfile: { endpoint: 'tms.tencentcloudapi.com' } }
Expand Down
11 changes: 7 additions & 4 deletions src/server/self-hosted/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ const getUserIP = require('get-user-ip')
const Lfsa = require('lokijs/src/loki-fs-structured-adapter')
const { v4: uuidv4 } = require('uuid') // 用户 id 生成
const {
$,
md5,
xml2js
getCheerio,
getDomPurify,
getMd5,
getXml2js
} = require('twikoo-func/utils/lib')
const {
getFuncVersion,
Expand Down Expand Up @@ -48,9 +49,11 @@ const { postCheckSpam } = require('twikoo-func/utils/spam')
const { sendNotice, emailTest } = require('twikoo-func/utils/notify')
const { uploadImage } = require('twikoo-func/utils/image')
const logger = require('twikoo-func/utils/logger')
const { getDomPurify } = require('twikoo-func/utils/dom')

const $ = getCheerio()
const DOMPurify = getDomPurify()
const md5 = getMd5()
const xml2js = getXml2js()

// 常量 / constants
const { RES_CODE, MAX_REQUEST_TIMES } = require('twikoo-func/utils/constants')
Expand Down
9 changes: 6 additions & 3 deletions src/server/self-hosted/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const getUserIP = require('get-user-ip')
const { URL } = require('url')
const { v4: uuidv4 } = require('uuid') // 用户 id 生成
const {
$,
getCheerio,
getDomPurify,
md5,
xml2js
getMd5,
getXml2js
} = require('twikoo-func/utils/lib')
const {
getFuncVersion,
Expand Down Expand Up @@ -48,7 +48,10 @@ const { sendNotice, emailTest } = require('twikoo-func/utils/notify')
const { uploadImage } = require('twikoo-func/utils/image')
const logger = require('twikoo-func/utils/logger')

const $ = getCheerio()
const DOMPurify = getDomPurify()
const md5 = getMd5()
const xml2js = getXml2js()

// 常量 / constants
const { RES_CODE, MAX_REQUEST_TIMES } = require('twikoo-func/utils/constants')
Expand Down
14 changes: 9 additions & 5 deletions src/server/vercel/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ const getUserIP = require('get-user-ip')
const { URL } = require('url')
const { v4: uuidv4 } = require('uuid') // 用户 id 生成
const {
$,
axios,
md5,
xml2js
getCheerio,
getAxios,
getDomPurify,
getMd5,
getXml2js
} = require('twikoo-func/utils/lib')
const {
getFuncVersion,
Expand Down Expand Up @@ -47,9 +48,12 @@ const { postCheckSpam } = require('twikoo-func/utils/spam')
const { sendNotice, emailTest } = require('twikoo-func/utils/notify')
const { uploadImage } = require('twikoo-func/utils/image')
const logger = require('twikoo-func/utils/logger')
const { getDomPurify } = require('twikoo-func/utils/dom')

const $ = getCheerio()
const axios = getAxios()
const DOMPurify = getDomPurify()
const md5 = getMd5()
const xml2js = getXml2js()

// 常量 / constants
const { RES_CODE, MAX_REQUEST_TIMES } = require('twikoo-func/utils/constants')
Expand Down

0 comments on commit 113e678

Please sign in to comment.