1- import * as utils from '../utils/utils.js' ; // 使用 import 引入工具类
21import { readFile } from 'fs/promises' ;
3- import vm from 'vm' ; // Node.js 的 vm 模块
4-
5- const { req} = await import ( '../utils/req.js' ) ;
6- const { sleep, sleepSync} = await import ( '../utils/utils.js' ) ;
7-
8- // 缓存已初始化的模块
2+ import crypto from 'crypto' ;
3+ import vm from 'vm' ;
4+ import * as utils from '../utils/utils.js' ;
5+ // const { req } = await import('../utils/req.js');
6+ import { matchesAll , stringUtils , cut } from '../libs_drpy/external.js'
7+ import { gbkTool } from '../libs_drpy/gbk.js'
8+ import { atob , btoa } from "../libs_drpy/crypto-util.js" ;
9+ import template from '../libs_drpy/template.js'
10+ import '../libs_drpy/drpyInject.js'
11+ import '../libs_drpy/crypto-js.js' ;
12+ import '../libs_drpy/jsencrypt.js' ;
13+ import '../libs_drpy/node-rsa.js' ;
14+ import '../libs_drpy/pako.min.js' ;
15+ import '../libs_drpy/json5.js'
16+ import '../libs_drpy/jinja.js'
17+
18+ const { sleep, sleepSync} = utils
19+
20+ // 缓存已初始化的模块和文件 hash 值
921const moduleCache = new Map ( ) ;
1022
23+ /**
24+ * 计算文件内容的 hash 值
25+ * @param {string } content - 文件内容
26+ * @returns {string } - 文件内容的 hash 值
27+ */
28+ function computeHash ( content ) {
29+ return crypto . createHash ( 'sha256' ) . update ( content , 'utf8' ) . digest ( 'hex' ) ;
30+ }
31+
1132/**
1233 * 初始化模块:加载并执行模块文件,存储初始化后的 rule 对象
1334 * 如果存在 `预处理` 属性且为函数,会在缓存前执行
@@ -16,24 +37,75 @@ const moduleCache = new Map();
1637 * @returns {Promise<object> } - 返回初始化后的模块对象
1738 */
1839export 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- }
23-
2440 try {
25- let t1 = utils . getNowTime ( ) ;
26- // 读取 JS 文件的内容
41+ // 读取文件内容
2742 const fileContent = await readFile ( filePath , 'utf-8' ) ;
2843
29- // 创建一个沙箱上下文,注入需要的全局变量和函数
30- const sandbox = {
31- console,
32- req,
44+ // 计算文件的 hash 值
45+ const fileHash = computeHash ( fileContent ) ;
46+
47+ // 检查缓存:是否有文件且未刷新且文件 hash 未变化
48+ if ( moduleCache . has ( filePath ) && ! refresh ) {
49+ const cached = moduleCache . get ( filePath ) ;
50+ if ( cached . hash === fileHash ) {
51+ console . log ( `Module ${ filePath } already initialized and unchanged, returning cached instance.` ) ;
52+ return cached . moduleObject ;
53+ }
54+ }
55+
56+ console . log ( `Loading module: ${ filePath } ` ) ;
57+ let t1 = utils . getNowTime ( ) ;
58+ const utilsSanbox = {
3359 sleep,
3460 sleepSync,
3561 utils,
62+ }
63+ const drpySanbox = {
64+ jsp,
65+ pdfh,
66+ pd,
67+ pdfa,
68+ pdfl,
69+ pjfh,
70+ pj,
71+ pjfa,
72+ base64Encode,
73+ base64Decode,
74+ local,
75+ md5X,
76+ rsaX,
77+ aesX,
78+ desX,
79+ req,
80+ JSProxyStream,
81+ JSFile,
82+ js2Proxy,
83+
84+ }
85+
86+ const libsSanbox = {
87+ matchesAll,
88+ stringUtils,
89+ cut,
90+ gbkTool,
91+ CryptoJS,
92+ JSEncrypt,
93+ NODERSA ,
94+ pako,
95+ JSON5 ,
96+ jinja,
97+ template,
98+ atob,
99+ btoa,
100+ }
101+
102+ // 创建一个沙箱上下文,注入需要的全局变量和函数
103+ const sandbox = {
104+ console,
36105 rule : { } , // 用于存放导出的 rule 对象
106+ ...utilsSanbox ,
107+ ...drpySanbox ,
108+ ...libsSanbox ,
37109 } ;
38110
39111 // 创建一个上下文
@@ -52,56 +124,61 @@ export async function init(filePath, refresh) {
52124 await moduleObject . 预处理 ( ) ;
53125 }
54126
55- // 缓存初始化后的模块
56- moduleCache . set ( filePath , moduleObject ) ;
57-
58127 let t2 = utils . getNowTime ( ) ;
59128 moduleObject . cost = t2 - t1 ;
60129
130+ // 缓存模块和文件的 hash 值
131+ moduleCache . set ( filePath , { moduleObject, hash : fileHash } ) ;
132+
61133 return moduleObject ;
62134 } catch ( error ) {
63135 console . error ( 'Error in drpy.init:' , error ) ;
64136 throw new Error ( 'Failed to initialize module' ) ;
65137 }
66138}
67139
140+
68141/**
69142 * 调用模块的指定方法
70143 * @param {string } filePath - 模块文件路径
71144 * @param {string } method - 要调用的属性方法名称
145+ * @param args
72146 * @returns {Promise<any> } - 方法调用的返回值
73147 */
74- async function invokeMethod ( filePath , method ) {
148+ async function invokeMethod ( filePath , method , ... args ) {
75149 const moduleObject = await init ( filePath ) ; // 确保模块已初始化
76150 if ( moduleObject [ method ] && typeof moduleObject [ method ] === 'function' ) {
77- return await moduleObject [ method ] ( ) ; // 调用对应的方法
151+ return await moduleObject [ method ] ( ... args ) ; // 调用对应的方法并传递参数
78152 } else {
79153 throw new Error ( `Method ${ method } not found in module ${ filePath } ` ) ;
80154 }
81155}
82156
83157// 各种接口调用方法
84158
85- export async function home ( filePath ) {
86- return await invokeMethod ( filePath , 'class_parse' ) ;
159+ export async function home ( filePath , filter = 1 ) {
160+ return await invokeMethod ( filePath , 'class_parse' , { filter } ) ;
87161}
88162
89163export async function homeVod ( filePath ) {
90164 return await invokeMethod ( filePath , '推荐' ) ;
91165}
92166
93- export async function cate ( filePath ) {
94- return await invokeMethod ( filePath , '一级' ) ;
167+ export async function cate ( filePath , tid , pg = 1 , filter = 1 , extend = { } ) {
168+ return await invokeMethod ( filePath , '一级' , { tid , pg , filter , extend } ) ;
95169}
96170
97- export async function detail ( filePath ) {
98- return await invokeMethod ( filePath , '二级' ) ;
171+ export async function detail ( filePath , ids ) {
172+ if ( ! Array . isArray ( ids ) ) throw new Error ( 'Parameter "ids" must be an array' ) ;
173+ return await invokeMethod ( filePath , '二级' , { ids} ) ;
99174}
100175
101- export async function search ( filePath ) {
102- return await invokeMethod ( filePath , '搜索' ) ;
176+ export async function search ( filePath , wd , quick = 0 , pg = 1 ) {
177+ return await invokeMethod ( filePath , '搜索' , { wd , quick , pg } ) ;
103178}
104179
105- export async function play ( filePath ) {
106- return await invokeMethod ( filePath , 'lazy' ) ;
180+ export async function play ( filePath , flag , id , flags ) {
181+ flags = flags || [ ] ;
182+ if ( ! Array . isArray ( flags ) ) throw new Error ( 'Parameter "flags" must be an array' ) ;
183+ return await invokeMethod ( filePath , 'lazy' , { flag, id, flags} ) ;
107184}
0 commit comments