-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathdrpy.js
107 lines (87 loc) · 3.28 KB
/
drpy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import * as utils from '../utils/utils.js'; // 使用 import 引入工具类
import {readFile} from 'fs/promises';
import vm from 'vm'; // Node.js 的 vm 模块
const {req} = await import('../utils/req.js');
const {sleep, sleepSync} = await import('../utils/utils.js');
// 缓存已初始化的模块
const moduleCache = new Map();
/**
* 初始化模块:加载并执行模块文件,存储初始化后的 rule 对象
* 如果存在 `预处理` 属性且为函数,会在缓存前执行
* @param {string} filePath - 模块文件路径
* @param refresh 强制清除缓存
* @returns {Promise<object>} - 返回初始化后的模块对象
*/
export async function init(filePath, refresh) {
if (moduleCache.has(filePath) && !refresh) {
console.log(`Module ${filePath} already initialized, returning cached instance.`);
return moduleCache.get(filePath);
}
try {
let t1 = utils.getNowTime();
// 读取 JS 文件的内容
const fileContent = await readFile(filePath, 'utf-8');
// 创建一个沙箱上下文,注入需要的全局变量和函数
const sandbox = {
console,
req,
sleep,
sleepSync,
utils,
rule: {}, // 用于存放导出的 rule 对象
};
// 创建一个上下文
const context = vm.createContext(sandbox);
// 执行文件内容,将其放入沙箱中
const script = new vm.Script(fileContent);
script.runInContext(context);
// 访问沙箱中的 rule 对象
const moduleObject = utils.deepCopy(sandbox.rule);
// 检查并执行 `预处理` 方法
if (typeof moduleObject.预处理 === 'function') {
console.log('Executing preprocessing...');
await moduleObject.预处理();
}
// 缓存初始化后的模块
moduleCache.set(filePath, moduleObject);
let t2 = utils.getNowTime();
moduleObject.cost = t2 - t1;
return moduleObject;
} catch (error) {
console.error('Error in drpy.init:', error);
throw new Error('Failed to initialize module');
}
}
/**
* 调用模块的指定方法
* @param {string} filePath - 模块文件路径
* @param {string} method - 要调用的属性方法名称
* @returns {Promise<any>} - 方法调用的返回值
*/
async function invokeMethod(filePath, method) {
const moduleObject = await init(filePath); // 确保模块已初始化
if (moduleObject[method] && typeof moduleObject[method] === 'function') {
return await moduleObject[method](); // 调用对应的方法
} else {
throw new Error(`Method ${method} not found in module ${filePath}`);
}
}
// 各种接口调用方法
export async function home(filePath) {
return await invokeMethod(filePath, 'class_parse');
}
export async function homeVod(filePath) {
return await invokeMethod(filePath, '推荐');
}
export async function cate(filePath) {
return await invokeMethod(filePath, '一级');
}
export async function detail(filePath) {
return await invokeMethod(filePath, '二级');
}
export async function search(filePath) {
return await invokeMethod(filePath, '搜索');
}
export async function play(filePath) {
return await invokeMethod(filePath, 'lazy');
}