Skip to content

Commit

Permalink
Feat perf and fix (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
danni-cool authored Jan 11, 2024
2 parents 48798da + cd1288e commit 26148f9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# 启动服务的端口
PORT=3001

# 运行时提示的消息等级(默认info,想有更详细的日志,可以指定为debug)
LOG_LEVEL=info

# 如果不希望登录一次后就记住当前账号,想每次都扫码登陆,填 true
DISABLE_AUTO_LOGIN=

Expand Down
6 changes: 4 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const Utils = require('./src/utils/index')
Utils.proxyConsole()
require('dotenv').config({
path: process.env.homeEnvCfg /** 兼容cli调用 */ ?? './.env'
})
/** log 在 prestart 阶段初始化了,后续需要手动改level才能同步env配置 */
require('./src/utils/index').proxyConsole({
logLevel: process.env.LOG_LEVEL
})
const { PORT } = process.env
const { Hono } = require('hono')
const { serve } = require('@hono/node-server')
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"chalk": "^4.1.2",
"dotenv": "^16.3.1",
"file-box": "1.4.15",
"file-type": "^18.5.0",
"form-data": "^4.0.0",
"hono": "^3.11.11",
"log4js": "^6.9.1",
Expand Down
33 changes: 0 additions & 33 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 8 additions & 41 deletions src/service/msgUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ const { LOCAL_RECVD_MSG_API, RECVD_MSG_API } = process.env
const { MSG_TYPE_ENUM } = require('../config/const')
const cacheTool = require('../service/cache')

/** @type {import('file-type').fileTypeFromBuffer} */
let fileTypeFromBuffer

import('file-type')
.then((res) => {
fileTypeFromBuffer = res.fileTypeFromBuffer
})
.catch((e) => {
Utils.logger.error('error', '导入file-type发生异常', e)
})

/**
* 收到消息上报接受url
* @typedef {{type:'text'|'fileUrl'}} baseMsgInterface
Expand Down Expand Up @@ -117,36 +106,14 @@ async function sendMsg2RecvdApi(msg) {
/**@type {import('file-box').FileBox} */
//@ts-expect-errors 这里msg一定是wechaty的msg
const steamFile = await msg.toFileBox()
const type = await fileTypeFromBuffer(
// wechaty dont defined this property as Unit8Array or ArrayBuffer
//@ts-expect-errors 需要用到私有属性
steamFile.buffer ?? steamFile.stream
)
let fileInfo = { ext: '', mime: '', filename: '' }

// 文件类型尝试解析
if (type !== undefined) {
fileInfo = {
// _name:'unknown.txt' => unknown.jpg
filename:
steamFile.mimeType ===
'application/unknown' /** 截图等无法推断出文件名会变成 unknown.txt */
? `${Date.now()}.${type.ext}`
: //@ts-expect-errors 需要用到私有属性
steamFile._name ?? `${Date.now()}.${type.ext}`,
ext: type.ext,
mime: type.mime
}
// 解析不出来尝试用文件后缀名
} else {
fileInfo = {
//@ts-expect-errors 需要用到私有属性
filename: steamFile._name ?? 'UnknownFile',
//@ts-expect-errors 需要用到私有属性
ext: steamFile._name.split('.').pop() ?? '',
//@ts-expect-errors 需要用到私有属性
mime: steamFile._mediaType
}

let fileInfo = {
// @ts-ignore
ext: steamFile._name.split('.').pop() ?? '',
// @ts-ignore
mime: steamFile._mediaType ?? 'Unknown',
// @ts-ignore
filename: steamFile._name ?? 'UnknownFile'
}

formData.append(
Expand Down
8 changes: 6 additions & 2 deletions src/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ if (!process.env.homeEnvCfg) {
const originalConsoleLog = console.log
const originalConsoleWarn = console.warn
const originalConsoleErr = console.error

const proxyConsole = () => {
/**
*
* @param {{logLevel?:string}} param0
*/
const proxyConsole = ({ logLevel = 'info' } = {}) => {
logger.level = logLevel
/**
* 希望排除在log4js里的console输出,即不希望打到日志里去或者显示异常
* @param {any[]} args
Expand Down

0 comments on commit 26148f9

Please sign in to comment.