From b71e1271c3008c53c4ebf3ef3c4de50fcbce1dc0 Mon Sep 17 00:00:00 2001 From: gdjdkid <67935269+gdjdkid@users.noreply.github.com> Date: Sat, 21 Mar 2026 16:40:28 +0800 Subject: [PATCH] Fix: Send requests using Node.js's native HTTPS, without relying on curl, and with cross-platform compatibility. --- lib/fetcher.js | 43 +++++++++++++++++++++++++++++-------------- package.json | 2 +- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/fetcher.js b/lib/fetcher.js index 8d86912..644aa65 100644 --- a/lib/fetcher.js +++ b/lib/fetcher.js @@ -1,4 +1,4 @@ -import { execSync, spawnSync } from 'child_process'; +import { execSync } from 'child_process'; import * as cheerio from 'cheerio'; import { getCache, setCache, getConfig } from './cache.js'; import chalk from 'chalk'; @@ -157,21 +157,36 @@ async function searchJavdb(id) { const session = decodeURIComponent(config.session); const cookie = `locale=zh; over18=1; _jdb_session=${session}`; - // 用 spawnSync 传数组参数,完全不经过 shell - // 特殊字符 +/= 直接传给 curl,不会被 shell 破坏 - function fetchJavdb(url) { - const result = spawnSync('curl', [ - '-sL', url, - '-H', 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36', - '-H', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', - '-H', 'Accept-Language: zh-CN,zh;q=0.9', - '-H', `Cookie: ${cookie}`, - ], { timeout: 15000, maxBuffer: 1024 * 1024 * 10 }); - return result.stdout.toString(); + // 用 Node.js 原生 https 发请求,不依赖 curl,跨平台兼容 + async function fetchJavdbHtml(url) { + const { default: https } = await import('https'); + const { default: http } = await import('http'); + return new Promise((resolve, reject) => { + const client = url.startsWith('https') ? https : http; + const req = client.get(url, { + headers: { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36', + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', + 'Accept-Language': 'zh-CN,zh;q=0.9', + 'Cookie': cookie, + }, + }, (res) => { + // 处理重定向 + if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { + resolve(fetchJavdbHtml(res.headers.location)); + return; + } + const chunks = []; + res.on('data', chunk => chunks.push(chunk)); + res.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8'))); + }); + req.on('error', reject); + req.setTimeout(15000, () => { req.destroy(); reject(new Error('timeout')); }); + }); } const searchUrl = `https://javdb.com/search?q=${encodeURIComponent(id)}&f=all`; - const searchHtml = fetchJavdb(searchUrl); + const searchHtml = await fetchJavdbHtml(searchUrl); const $ = cheerio.load(searchHtml); let detailPath = null; @@ -192,7 +207,7 @@ async function searchJavdb(id) { let detailHtml = ''; for (let i = 0; i < 3; i++) { await new Promise(resolve => setTimeout(resolve, 2000)); - detailHtml = fetchJavdb(`https://javdb.com${detailPath}`); + detailHtml = await fetchJavdbHtml(`https://javdb.com${detailPath}`); if (detailHtml.length > 1000) break; } diff --git a/package.json b/package.json index 68cb5b6..6c9cdd5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "javinfo", - "version": "1.1.9", + "version": "1.1.10", "description": "AV番号命令行查询工具", "main": "./index.js", "type": "module",