Skip to content

Commit 08e497d

Browse files
author
Taois
committed
feat: 框架完美处理
1 parent d484095 commit 08e497d

File tree

14 files changed

+134
-27
lines changed

14 files changed

+134
-27
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ nodejs作为服务端的drpy实现。全面升级异步写法
7171

7272
## 更新记录
7373

74+
### 20260315
75+
76+
更新至V1.3.29
77+
7478
### 20260314
7579

7680
更新至V1.3.28

docs/updateRecord.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# drpyS更新记录
22

3+
### 20260315
4+
5+
更新至V1.3.29
6+
7+
1. 彻底解决ds代码问题导致的阻止程序退出
8+
2. 完善全局require处理,支持cjs文件里引入 `axios` 等对象不报错
9+
3. 彻底优化本地bundle包
10+
11+
注意: 此版本为纯框架更新,不涉及源,只关注源的用户可以不用升级
12+
313
### 20260314
414

515
更新至V1.3.28

drpy-node-bundle/libs/localDsCore.bundled.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import process$1 from "node:process";
3232
import os from "node:os";
3333
import tty from "node:tty";
3434
import util$1 from "node:util";
35-
import { createRequire as createRequire$1 } from "module";
35+
import Module, { createRequire as createRequire$1 } from "module";
3636
import { setTimeout as setTimeout$1 } from "timers";
3737
import { pipeline } from "stream/promises";
3838
import diagnosticsChannel from "diagnostics_channel";
@@ -14367,17 +14367,19 @@ let debounceTimers = /* @__PURE__ */ new Map();
1436714367
function startJsonWatcher(ENGINES, jsonDir) {
1436814368
if (process.env.NODE_ENV !== "development") return;
1436914369
try {
14370-
watch(jsonDir, { recursive: true }, (eventType, filename) => {
14370+
jsonWatcher = watch(jsonDir, { recursive: true }, (eventType, filename) => {
1437114371
if (filename && filename.endsWith(".json")) {
1437214372
if (debounceTimers.has(filename)) clearTimeout(debounceTimers.get(filename));
1437314373
const timer = setTimeout(() => {
1437414374
console.log(`[HotReload] ${filename} changed, clearing cache...`);
1437514375
ENGINES.drpyS.clearAllCache();
1437614376
debounceTimers.delete(filename);
1437714377
}, 100);
14378+
if (timer.unref) timer.unref();
1437814379
debounceTimers.set(filename, timer);
1437914380
}
1438014381
});
14382+
if (jsonWatcher.unref) jsonWatcher.unref();
1438114383
} catch (error) {
1438214384
console.error("start json file listening failed with error:", error);
1438314385
}
@@ -374920,6 +374922,20 @@ const __dirname$4 = path.dirname(fileURLToPath(import.meta.url));
374920374922
const ROOT_DIR = path.resolve(__dirname$4, "../");
374921374923
const LIB_ROOT = path.join(ROOT_DIR, "spider/js");
374922374924
const customRequire = createRequire$1(import.meta.url);
374925+
/**
374926+
* Patch Module.prototype.require to handle bundled modules in CJS context
374927+
* This allows native CJS modules (like those loaded via require('./lib.js')) to find
374928+
* bundled dependencies (axios, iconv-lite, etc.) which are exposed on globalThis.
374929+
*/
374930+
const originalRequire = Module.prototype.require;
374931+
Module.prototype.require = function(id) {
374932+
if (id === "iconv-lite" && globalThis.iconv) return globalThis.iconv;
374933+
if (id === "axios" && globalThis.axios) return globalThis.axios;
374934+
if (id === "cheerio" && globalThis.cheerio) return globalThis.cheerio;
374935+
if (id === "qs" && globalThis.qs) return globalThis.qs;
374936+
if (id === "crypto-js" && globalThis.CryptoJS) return globalThis.CryptoJS;
374937+
return originalRequire.apply(this, arguments);
374938+
};
374923374939
const rootRequire = (modulePath) => {
374924374940
if (modulePath === "iconv-lite") return globalThis.iconv;
374925374941
if (modulePath === "axios") return globalThis.axios;
@@ -393934,6 +393950,11 @@ var catvod_default = {
393934393950
};
393935393951
//#endregion
393936393952
//#region localDsCore.js
393953+
const ENGINES = {
393954+
drpyS: drpyS_exports,
393955+
php: php_default,
393956+
catvod: catvod_default
393957+
};
393937393958
const __dirname$1 = path.dirname(fileURLToPath(import.meta.url));
393938393959
const isBundled = __dirname$1.endsWith("dist") || __dirname$1.endsWith("libs") || __dirname$1.endsWith("dist" + path.sep) || __dirname$1.endsWith("libs" + path.sep);
393939393960
const possibleRoots = [
@@ -393962,15 +393983,7 @@ const options = {
393962393983
catDir,
393963393984
catLibDir
393964393985
};
393965-
/**
393966-
* 支持的引擎映射表
393967-
* 包含drpyS、php、catvod
393968-
*/
393969-
const ENGINES = {
393970-
drpyS: drpyS_exports,
393971-
php: php_default,
393972-
catvod: catvod_default
393973-
};
393986+
startJsonWatcher(ENGINES, jsonDir);
393974393987
/**
393975393988
* 创建带超时的Promise包装函数
393976393989
* 为API操作添加超时控制,防止长时间阻塞
@@ -393987,9 +394000,10 @@ function withTimeout(promise, timeoutMs = null, operation = "API操作", invokeM
393987394000
else defaultTimeout = 20 * 1e3;
393988394001
const actualTimeout = timeoutMs || defaultTimeout;
393989394002
return Promise.race([promise, new Promise((_, reject) => {
393990-
setTimeout(() => {
394003+
const timer = setTimeout(() => {
393991394004
reject(/* @__PURE__ */ new Error(`${operation}超时 (${actualTimeout}ms)`));
393992394005
}, actualTimeout);
394006+
if (timer.unref) timer.unref();
393993394007
})]);
393994394008
}
393995394009
async function getEngine(moduleName, query, inject_env) {

drpy-node-bundle/localDsCore.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import * as drpyS from '../libs/drpyS.js';
77
import php from '../libs/php.js';
88
import catvod from '../libs/catvod.js';
99

10+
// 初始化API引擎集合
11+
const ENGINES = {
12+
drpyS,
13+
php,
14+
catvod,
15+
};
16+
1017
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1118

1219
// Determine if we are running from the dist/libs directory (bundled) or source
@@ -42,15 +49,9 @@ const options = {
4249
catDir,
4350
catLibDir,
4451
};
45-
/**
46-
* 支持的引擎映射表
47-
* 包含drpyS、php、catvod
48-
*/
49-
const ENGINES = {
50-
drpyS,
51-
php,
52-
catvod,
53-
};
52+
53+
// 启动JSON文件监听器 (仅在开发环境下生效)
54+
startJsonWatcher(ENGINES, jsonDir);
5455
//const query = {
5556
// do: 'ds',
5657
// pg: 1,
@@ -83,9 +84,10 @@ function withTimeout(promise, timeoutMs = null, operation = 'API操作', invokeM
8384
return Promise.race([
8485
promise,
8586
new Promise((_, reject) => {
86-
setTimeout(() => {
87+
const timer = setTimeout(() => {
8788
reject(new Error(`${operation}超时 (${actualTimeout}ms)`));
8889
}, actualTimeout);
90+
if (timer.unref) timer.unref();
8991
})
9092
]);
9193
}

drpy-node-bundle/localDsCoreTest.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// import * as localtDsCore from './localDsCore.js';
2-
const start = performance.now();
32
import * as localtDsCore from './libs/localDsCore.bundled.js';
3+
const start = performance.now();
44

55
console.log(getEngine)
66
// const a = await getEngine('爱推图[画]', {}, {proxyUrl: "xx"})
@@ -21,4 +21,14 @@ const e = await getEngine('30wMV[听]', {
2121
// console.log(e)
2222
const end = performance.now();
2323

24-
console.log(`耗时: ${end - start} ms`);
24+
console.log(`耗时: ${end - start} ms`);
25+
// process.exit(0);
26+
27+
const f = await getEngine('央视大全[官]', {
28+
do: 'ds',
29+
proxy: 1,
30+
url: '',
31+
}, {requestHost: 'http://127.0.0.1:5757',proxyPath:'https://dh5wswx02.v.cntv.cn/asp/h5e/hls/2000/0303000a/3/default/b90af013c16e44de9e4f1f56dab91f63/1.ts'})
32+
33+
const [statuCode, contentType, buffer, headers] = f;
34+
console.log('resutl: header:', headers, 'buffer length:', buffer.length);

drpy-node-bundle/spider/js/_lib.cntv2026.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const axios = require('axios');
2+
// const iconv = require('iconv-lite');
23
// ==================== 常量定义 ====================
34
const TS_PACKET_SIZE = 188;
45
const VIDEO_PID = 256;

libs/drpyS.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ const CACHE_OPTIONS = {
7474
max: 100,
7575
ttl: 1000 * 60 * 10, // 10分钟
7676
};
77+
// const moduleCache = new Map();
78+
// const ruleObjectCache = new Map();
79+
// const jxCache = new Map();
7780
const moduleCache = new LRUCache(CACHE_OPTIONS);
7881
const ruleObjectCache = new LRUCache(CACHE_OPTIONS);
7982
const jxCache = new LRUCache(CACHE_OPTIONS);

libs_drpy/moduleLoader.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {readFileSync, existsSync} from 'fs';
22
import path from "path";
3-
import {createRequire} from 'module';
3+
import Module, {createRequire} from 'module';
44
import {fileURLToPath} from "url";
55
import axios from 'axios';
66
import fetchSync from 'sync-fetch';
@@ -11,6 +11,21 @@ const LIB_ROOT = path.join(ROOT_DIR, 'spider/js');
1111

1212
const customRequire = createRequire(import.meta.url);
1313

14+
/**
15+
* Patch Module.prototype.require to handle bundled modules in CJS context
16+
* This allows native CJS modules (like those loaded via require('./lib.js')) to find
17+
* bundled dependencies (axios, iconv-lite, etc.) which are exposed on globalThis.
18+
*/
19+
const originalRequire = Module.prototype.require;
20+
Module.prototype.require = function(id) {
21+
if (id === 'iconv-lite' && globalThis.iconv) return globalThis.iconv;
22+
if (id === 'axios' && globalThis.axios) return globalThis.axios;
23+
if (id === 'cheerio' && globalThis.cheerio) return globalThis.cheerio;
24+
if (id === 'qs' && globalThis.qs) return globalThis.qs;
25+
if (id === 'crypto-js' && globalThis.CryptoJS) return globalThis.CryptoJS;
26+
return originalRequire.apply(this, arguments);
27+
};
28+
1429
// 导出 rootRequire
1530
export const rootRequire = (modulePath) => {
1631
// 处理内置模块或打包模块

package-bundle.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {execSync} from 'child_process';
2-
import {existsSync, statSync} from 'fs';
2+
import {existsSync, statSync, readdirSync, copyFileSync, mkdirSync} from 'fs';
33
import {join, basename, dirname, resolve} from 'path';
44
import url from 'url';
55

@@ -15,6 +15,41 @@ const INCLUDE_ITEMS = [
1515
// 获取脚本所在目录 (e:\gitwork\drpy-node)
1616
const getScriptDir = () => dirname(resolve(url.fileURLToPath(import.meta.url)));
1717

18+
// 复制 _lib 开头的文件到 bundle 目录
19+
const copyLibFiles = (scriptDir) => {
20+
const sourceDir = join(scriptDir, 'spider', 'js');
21+
const targetDir = join(scriptDir, 'drpy-node-bundle', 'spider', 'js');
22+
23+
if (!existsSync(sourceDir)) {
24+
console.warn(`警告: 源目录 ${sourceDir} 不存在,跳过复制 lib 文件。`);
25+
return;
26+
}
27+
28+
if (!existsSync(targetDir)) {
29+
console.log(`创建目标目录: ${targetDir}`);
30+
mkdirSync(targetDir, {recursive: true});
31+
}
32+
33+
console.log(`正在从 ${sourceDir} 复制 lib 文件到 ${targetDir}...`);
34+
35+
try {
36+
const files = readdirSync(sourceDir);
37+
let count = 0;
38+
for (const file of files) {
39+
if (file.startsWith('_lib') && (file.endsWith('.js') || file.endsWith('.cjs'))) {
40+
const srcPath = join(sourceDir, file);
41+
const destPath = join(targetDir, file);
42+
copyFileSync(srcPath, destPath);
43+
// console.log(`已复制: ${file}`);
44+
count++;
45+
}
46+
}
47+
console.log(`成功复制了 ${count} 个 lib 文件。`);
48+
} catch (error) {
49+
console.error(`复制 lib 文件失败: ${error.message}`);
50+
}
51+
};
52+
1853
// 压缩 drpy-node-bundle 目录
1954
const compressBundle = (scriptDir) => {
2055
// drpy-node-bundle 目录路径
@@ -79,6 +114,7 @@ const compressBundle = (scriptDir) => {
79114
// 主程序入口
80115
const main = () => {
81116
const scriptDir = getScriptDir();
117+
copyLibFiles(scriptDir);
82118
compressBundle(scriptDir);
83119
};
84120

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drpy-node",
3-
"version": "1.3.28",
3+
"version": "1.3.29",
44
"main": "index.js",
55
"type": "module",
66
"scripts": {

0 commit comments

Comments
 (0)