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