Skip to content

Commit

Permalink
Allow dependency injection for nodemailer and DOMPurify (#699)
Browse files Browse the repository at this point in the history
Co-authored-by: Tao Xin <alexander.xin@gmail.com>
  • Loading branch information
Tao-VanJS and alexander-xin authored May 20, 2024
1 parent 5ff1a33 commit 5cb7be5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/server/function/twikoo/utils/lib.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
let customLibs = {}

module.exports = {
setCustomLibs (libs) {
customLibs = libs
},
getCheerio () {
const $ = require('cheerio') // jQuery 服务器版
return $
Expand All @@ -24,6 +29,7 @@ module.exports = {
return bowser
},
getDomPurify () {
if (customLibs.DOMPurify) return customLibs.DOMPurify
// 初始化反 XSS
const { JSDOM } = require('jsdom') // document.window 服务器版
const createDOMPurify = require('dompurify') // 反 XSS
Expand All @@ -44,6 +50,7 @@ module.exports = {
return md5
},
getNodemailer () {
if (customLibs.nodemailer) return customLibs.nodemailer
const nodemailer = require('nodemailer') // 发送邮件
return nodemailer
},
Expand Down
9 changes: 7 additions & 2 deletions src/server/function/twikoo/utils/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ const {
getPushoo
} = require('./lib')
const $ = getCheerio()
const nodemailer = getNodemailer()
const pushoo = getPushoo()
const { RES_CODE } = require('./constants')
const logger = require('./logger')

let nodemailer

function lazilyGetNodemailer () {
return nodemailer ?? (nodemailer = getNodemailer())
}

let transporter

const fn = {
Expand Down Expand Up @@ -45,7 +50,7 @@ const fn = {
} else {
throw new Error('SMTP 服务器没有配置')
}
transporter = nodemailer.createTransport(transportConfig)
transporter = lazilyGetNodemailer().createTransport(transportConfig)
try {
const success = await transporter.verify()
if (success) logger.info('SMTP 邮箱配置正常')
Expand Down

0 comments on commit 5cb7be5

Please sign in to comment.