1
- import * as utils from '../utils/utils.js' ; // 使用 import 引入工具类
2
1
import { 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 值
9
21
const moduleCache = new Map ( ) ;
10
22
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
+
11
32
/**
12
33
* 初始化模块:加载并执行模块文件,存储初始化后的 rule 对象
13
34
* 如果存在 `预处理` 属性且为函数,会在缓存前执行
@@ -16,24 +37,75 @@ const moduleCache = new Map();
16
37
* @returns {Promise<object> } - 返回初始化后的模块对象
17
38
*/
18
39
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
- }
23
-
24
40
try {
25
- let t1 = utils . getNowTime ( ) ;
26
- // 读取 JS 文件的内容
41
+ // 读取文件内容
27
42
const fileContent = await readFile ( filePath , 'utf-8' ) ;
28
43
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 = {
33
59
sleep,
34
60
sleepSync,
35
61
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,
36
105
rule : { } , // 用于存放导出的 rule 对象
106
+ ...utilsSanbox ,
107
+ ...drpySanbox ,
108
+ ...libsSanbox ,
37
109
} ;
38
110
39
111
// 创建一个上下文
@@ -52,56 +124,61 @@ export async function init(filePath, refresh) {
52
124
await moduleObject . 预处理 ( ) ;
53
125
}
54
126
55
- // 缓存初始化后的模块
56
- moduleCache . set ( filePath , moduleObject ) ;
57
-
58
127
let t2 = utils . getNowTime ( ) ;
59
128
moduleObject . cost = t2 - t1 ;
60
129
130
+ // 缓存模块和文件的 hash 值
131
+ moduleCache . set ( filePath , { moduleObject, hash : fileHash } ) ;
132
+
61
133
return moduleObject ;
62
134
} catch ( error ) {
63
135
console . error ( 'Error in drpy.init:' , error ) ;
64
136
throw new Error ( 'Failed to initialize module' ) ;
65
137
}
66
138
}
67
139
140
+
68
141
/**
69
142
* 调用模块的指定方法
70
143
* @param {string } filePath - 模块文件路径
71
144
* @param {string } method - 要调用的属性方法名称
145
+ * @param args
72
146
* @returns {Promise<any> } - 方法调用的返回值
73
147
*/
74
- async function invokeMethod ( filePath , method ) {
148
+ async function invokeMethod ( filePath , method , ... args ) {
75
149
const moduleObject = await init ( filePath ) ; // 确保模块已初始化
76
150
if ( moduleObject [ method ] && typeof moduleObject [ method ] === 'function' ) {
77
- return await moduleObject [ method ] ( ) ; // 调用对应的方法
151
+ return await moduleObject [ method ] ( ... args ) ; // 调用对应的方法并传递参数
78
152
} else {
79
153
throw new Error ( `Method ${ method } not found in module ${ filePath } ` ) ;
80
154
}
81
155
}
82
156
83
157
// 各种接口调用方法
84
158
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 } ) ;
87
161
}
88
162
89
163
export async function homeVod ( filePath ) {
90
164
return await invokeMethod ( filePath , '推荐' ) ;
91
165
}
92
166
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 } ) ;
95
169
}
96
170
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} ) ;
99
174
}
100
175
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 } ) ;
103
178
}
104
179
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} ) ;
107
184
}
0 commit comments