Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使twikoo支持Cloudflare worker的兼容性改动 #695

Merged
merged 5 commits into from
May 14, 2024

Conversation

Tao-VanJS
Copy link
Contributor

@Tao-VanJS Tao-VanJS commented May 13, 2024

这个PR包含了所有支持Cloudflare worker的兼容性改动。部署Cloudflare worker的源代码在这里:https://github.com/Tao-VanJS/twikoo-cloudflare ,亲测有效。

根据实测结果,相比Netlify/Vercel + MongoDB的部署,Cloudflare worker的部署把冷启动的页面加载时间从6秒左右降到了小于0.5秒,极大地提升了用户体验。

@Tao-VanJS
Copy link
Contributor Author

nodemailer发邮件的那部分代码应该是被这个PR改坏了。我现在正在将其修复,同时也会把发送邮件通知的功能加到Cloudflare worker部署里去。

Copy link
Member

@imaegoo imaegoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢贡献。

因为twikoo-func被多个部署方式所依赖,而本改动没有考虑其他部署方式,所以该PR的改动必然会带来问题。

cloudflare worker除了和JSDOM存在兼容性问题,是否还和其他第三方依赖存在兼容性问题?如果只是JSDOM,建议减小改动范围,只提取JSDOM。

src/server/function/twikoo/package.json Outdated Show resolved Hide resolved
src/server/function/twikoo/utils/spam.js Outdated Show resolved Hide resolved
src/server/function/twikoo/utils/lib.js Show resolved Hide resolved
@imaegoo
Copy link
Member

imaegoo commented May 14, 2024

如果您能确定cloudflare worker和哪些第三方依赖存在兼容问题,我可以完成其他部署方式的修改和测试。

@Tao-VanJS
Copy link
Contributor Author

如果您能确定cloudflare worker和哪些第三方依赖存在兼容问题,我可以完成其他部署方式的修改和测试。

目前确认如下模块与Cloudflare worker存在兼容性问题:

  • jsdom
  • @imaegoo/node-ip2region
  • nodemailer
  • tencentcloud-sdk-nodejs

参见:https://github.com/Tao-VanJS/twikoo-cloudflare?tab=readme-ov-file#known-limitations

现在这个PR和nodemailer的整合是有一点问题的,我现在正在修复。修好了会合并到这个PR里。

@imaegoo
Copy link
Member

imaegoo commented May 14, 2024

考虑一下 #696 这个方案是否能满足您的需求:将lib.js其中的第三方依赖统统包装成get方法:

lib.js

module.exports = {
  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
  }
}

这样,当twikoo-cloudflare依赖lib.js时,就不会因立即加载DOMPurify而报错了

仅在需要时导入第三方依赖,避免某一个依赖出现兼容性问题而无法导入其他依赖。
@imaegoo
Copy link
Member

imaegoo commented May 14, 2024

image

nodemailer 没问题了,合并中

@imaegoo imaegoo merged commit fe59c26 into twikoojs:main May 14, 2024
1 check passed
const logger = require('./logger')

let tencentcloud

function getTencentCloud () {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个getTencentCloud是不是和getTencentcloud名字太过接近了😁?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants