diff --git a/.eslintrc.json b/.eslintrc.json index 7a6853ff6..09396c834 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -117,7 +117,8 @@ "@typescript-eslint/no-var-requires": 0, "@typescript-eslint/no-explicit-any": 0, "n/no-missing-import": 0, - "n/no-unpublished-import": 0 + "n/no-unpublished-import": 0, + "no-prototype-builtins": 0 }, "overrides": [ { diff --git a/.gitignore b/.gitignore index d2abfb301..a5c841183 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ coverage docs/.vuepress/dist node_modules tmp +dist Session.vim combined.log diff --git a/lib/middleware/access-control.ts b/lib/middleware/access-control.ts index 9d119fa59..11e4b92f5 100644 --- a/lib/middleware/access-control.ts +++ b/lib/middleware/access-control.ts @@ -1,7 +1,7 @@ import type { MiddlewareHandler } from 'hono'; import { config } from '@/config'; import md5 from '@/utils/md5'; -import isLocalhost from 'is-localhost-ip'; +import ipUtils from 'ip'; import { getIp } from '@/utils/helpers'; import RejectError from '@/errors/reject'; @@ -39,7 +39,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => { const isControlled = config.accessKey || config.allowlist || config.denylist; - const allowLocalhost = config.allowLocalhost && ip && (await isLocalhost(ip)); + const allowLocalhost = config.allowLocalhost && ip && ipUtils.isPrivate(ip); const grant = async () => { if (ctx.res.status !== 403) { diff --git a/lib/middleware/parameter.ts b/lib/middleware/parameter.ts index 8effc17d0..8700e281a 100644 --- a/lib/middleware/parameter.ts +++ b/lib/middleware/parameter.ts @@ -49,9 +49,18 @@ const summarizeArticle = async (articleText: string) => { }, }); + // @ts-expect-error custom field return response.data.choices[0].message.content; }; +const getAuthorString = (item) => { + let author = ''; + if (item.author) { + author = typeof item.author === 'string' ? item.author : item.author.map((i) => i.name).join(' '); + } + return author; +}; + const middleware: MiddlewareHandler = async (ctx, next) => { await next(); @@ -187,7 +196,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => { data.item = data.item.filter((item) => { const title = item.title || ''; const description = item.description || title; - const author = item.author || ''; + const author = getAuthorString(item); const category = item.category || []; const isFilter = regex instanceof RE2JS @@ -203,7 +212,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => { data.item = data.item.filter((item) => { const title = item.title || ''; const description = item.description || title; - const author = item.author || ''; + const author = getAuthorString(item); const category = item.category || []; let isFilter = true; @@ -232,7 +241,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => { data.item = data.item.filter((item) => { const title = item.title; const description = item.description || title; - const author = item.author || ''; + const author = getAuthorString(item); const category = item.category || []; let isFilter = true; @@ -295,6 +304,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => { if (link) { // if parser failed, return default description and not report error try { + // @ts-expect-error custom field const { data: res } = await got(link); const $ = load(res); const result = await Parser.parse(link, { diff --git a/lib/middleware/template.ts b/lib/middleware/template.ts index 7fdae7097..9fa7232ac 100644 --- a/lib/middleware/template.ts +++ b/lib/middleware/template.ts @@ -30,14 +30,14 @@ const middleware: MiddlewareHandler = async (ctx, next) => { const template = path.resolve(__dirname, `../views/${templateName}`); if (data) { - data.title = utils.collapseWhitespace(data.title); - data.description && (data.description = utils.collapseWhitespace(data.description)); - data.author && (data.author = utils.collapseWhitespace(data.author)); + data.title = utils.collapseWhitespace(data.title) || ''; + data.description && (data.description = utils.collapseWhitespace(data.description) || ''); + data.author && (data.author = utils.collapseWhitespace(data.author) || ''); if (data.item) { for (const item of data.item) { if (item.title) { - item.title = utils.collapseWhitespace(item.title); + item.title = utils.collapseWhitespace(item.title) || ''; // trim title length for (let length = 0, i = 0; i < item.title.length; i++) { length += Buffer.from(item.title[i]).length === 1 ? 1 : 2; @@ -49,10 +49,10 @@ const middleware: MiddlewareHandler = async (ctx, next) => { } if (typeof item.author === 'string') { - item.author = utils.collapseWhitespace(item.author); + item.author = utils.collapseWhitespace(item.author) || ''; } else if (typeof item.author === 'object' && item.author !== null) { for (const a of item.author) { - a.name = utils.collapseWhitespace(a.name); + a.name = utils.collapseWhitespace(a.name) || ''; } if (outputType !== 'json') { item.author = item.author.map((a: { name: string }) => a.name).join(', '); @@ -66,8 +66,8 @@ const middleware: MiddlewareHandler = async (ctx, next) => { } if (outputType !== 'rss') { - item.pubDate && (item.pubDate = utils.convertDateToISO8601(item.pubDate)); - item.updated && (item.updated = utils.convertDateToISO8601(item.updated)); + item.pubDate && (item.pubDate = utils.convertDateToISO8601(item.pubDate) || ''); + item.updated && (item.updated = utils.convertDateToISO8601(item.updated) || ''); } } } diff --git a/lib/utils/cache/memory.ts b/lib/utils/cache/memory.ts index 94a7ef31d..b7ef70c74 100644 --- a/lib/utils/cache/memory.ts +++ b/lib/utils/cache/memory.ts @@ -4,7 +4,7 @@ import type CacheModule from './base'; const status = { available: false }; -let memoryCache: LRUCache; +let memoryCache: LRUCache | undefined; export default { init: () => { @@ -15,7 +15,7 @@ export default { status.available = true; }, get: (key: string, refresh = true) => { - if (key && status.available) { + if (key && status.available && memoryCache) { let value = memoryCache.get(key, { updateAgeOnGet: refresh }) as string | undefined; if (value) { value = value + ''; @@ -30,7 +30,7 @@ export default { if (typeof value === 'object') { value = JSON.stringify(value); } - if (key && status.available) { + if (key && status.available && memoryCache) { return memoryCache.set(key, value, { ttl: maxAge * 1000 }); } }, diff --git a/lib/utils/cache/redis.ts b/lib/utils/cache/redis.ts index c27fd7d5a..73f0d63b7 100644 --- a/lib/utils/cache/redis.ts +++ b/lib/utils/cache/redis.ts @@ -3,7 +3,7 @@ import Redis from 'ioredis'; import logger from '@/utils/logger'; import type CacheModule from './base'; -let redisClient: Redis; +let redisClient: Redis | undefined; const status = { available: false }; @@ -33,7 +33,7 @@ export default { }); }, get: async (key: string, refresh = true) => { - if (key && status.available) { + if (key && status.available && redisClient) { const cacheTtlKey = getCacheTtlKey(key); let [value, cacheTtl] = await redisClient.mget(key, cacheTtlKey); if (value && refresh) { @@ -52,7 +52,7 @@ export default { } }, set: (key: string, value?: string | Record, maxAge = config.cache.contentExpire) => { - if (!status.available) { + if (!status.available || !redisClient) { return; } if (!value || value === 'undefined') { diff --git a/lib/utils/common-config.ts b/lib/utils/common-config.ts index ebb33baa4..65e84d209 100644 --- a/lib/utils/common-config.ts +++ b/lib/utils/common-config.ts @@ -43,9 +43,10 @@ async function buildData(data) { let charset = 'utf-8'; for (const attr of contentType.split(';')) { if (attr.includes('charset=')) { - charset = attr.split('=').pop(); + charset = attr.split('=').pop() || 'utf-8'; } } + // @ts-expect-error custom property const responseData = charset === 'utf-8' ? response.data : iconv.decode((await got.get({ url: data.url, responseType: 'buffer' })).data, charset); const $ = cheerio.load(responseData); const $item = $(data.item.item); diff --git a/lib/utils/puppeteer.ts b/lib/utils/puppeteer.ts index 5ff784439..9ad470f91 100644 --- a/lib/utils/puppeteer.ts +++ b/lib/utils/puppeteer.ts @@ -10,7 +10,7 @@ import StealthPlugin from 'puppeteer-extra-plugin-stealth'; const options = { args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-infobars', '--window-position=0,0', '--ignore-certificate-errors', '--ignore-certificate-errors-spki-list', `--user-agent=${config.ua}`], - headless: 'new' as const, + headless: true, ignoreHTTPSErrors: true, }; diff --git a/package.json b/package.json index 75c304030..ab26db487 100644 --- a/package.json +++ b/package.json @@ -20,9 +20,10 @@ "lib" ], "scripts": { - "build:all": "npm run build:radar && npm run build:maintainer", + "build:all": "npm run build && npm run build:radar && npm run build:maintainer", "build:maintainer": "node scripts/workflow/build-maintainer.js", "build:radar": "node scripts/workflow/build-radar.js", + "build": "tsc", "dev": "cross-env NODE_ENV=dev tsx watch lib/index.ts", "format": "eslint --cache --fix \"**/*.{ts,js,yml}\" && node website/docs/.format/format.mjs && prettier \"**/*.{ts,js,json}\" --write", "format:check": "eslint --cache \"**/*.{ts,js,yml}\" && prettier \"**/*.{ts,js,json}\" --check", @@ -106,8 +107,8 @@ "imapflow": "1.0.152", "instagram-private-api": "1.45.3", "ioredis": "5.3.2", + "ip": "2.0.1", "ip-regex": "4.3.0", - "is-localhost-ip": "2.0.0", "jsdom": "24.0.0", "json-bigint": "1.0.0", "json5": "2.2.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 34e59f7e2..b0477a415 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -98,12 +98,12 @@ dependencies: ioredis: specifier: 5.3.2 version: 5.3.2 + ip: + specifier: 2.0.1 + version: 2.0.1 ip-regex: specifier: 4.3.0 version: 4.3.0 - is-localhost-ip: - specifier: 2.0.0 - version: 2.0.0 jsdom: specifier: 24.0.0 version: 24.0.0 @@ -6302,8 +6302,8 @@ packages: resolution: {integrity: sha512-PuExPYUiu6qMBQb4l06ecm6T6ujzhmh+MeJcW9wa89PoAz5pvd4zPgN5WJV104mb6S2T1AwNIAaB70JNrLQWhg==} dev: false - /ip@2.0.0: - resolution: {integrity: sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==} + /ip@2.0.1: + resolution: {integrity: sha512-lJUL9imLTNi1ZfXT+DU6rBBdbiKGBuay9B6xGSPVjUeQwaH1RIGqef8RZkUtHioLmSNpPR5M4HVKJGm1j8FWVQ==} dev: false /is-alphabetical@1.0.4: @@ -6447,11 +6447,6 @@ packages: engines: {node: '>=0.10.0'} dev: false - /is-localhost-ip@2.0.0: - resolution: {integrity: sha512-vlgs2cSgMOfnKU8c1ewgKPyum9rVrjjLLW2HBdL5i0iAJjOs8NY55ZBd/hqUTaYR0EO9CKZd3hVSC2HlIbygTQ==} - engines: {node: '>=12'} - dev: false - /is-number@7.0.0: resolution: {integrity: sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==} engines: {node: '>=0.12.0'} @@ -10156,7 +10151,7 @@ packages: resolution: {integrity: sha512-7maUZy1N7uo6+WVEX6psASxtNlKaNVMlGQKkG/63nEDdLOWNbiUMoLK7X4uYoLhQstau72mLgfEWcXcwsaHbYQ==} engines: {node: '>= 10.13.0', npm: '>= 3.0.0'} dependencies: - ip: 2.0.0 + ip: 2.0.1 smart-buffer: 4.2.0 dev: false diff --git a/tsconfig.json b/tsconfig.json index 561edef6f..180b1826b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,8 @@ { "compilerOptions": { - "lib": ["ES2022"], + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "Bundler", "strict": true, "jsx": "react-jsx", "jsxImportSource": "hono/jsx", @@ -9,5 +11,8 @@ }, "esModuleInterop": true, "noImplicitAny": false, - } + "outDir": "./dist" + }, + "include": ["./lib/**/*"], + "exclude": ["node_modules", "*.test.*"] }