11import path from "path" ;
22import { readFile } from "fs/promises" ;
33import { getSitesMap } from "../utils/sites-map.js" ;
4- import { computeHash , getNowTime , deepCopy } from "../utils/utils.js" ;
4+ import { computeHash , deepCopy , getNowTime } from "../utils/utils.js" ;
55import { fileURLToPath , pathToFileURL } from 'url' ;
66import { md5 } from "../libs_drpy/crypto-util.js" ;
77
@@ -14,11 +14,6 @@ const _config_path = path.join(__dirname, '../config');
1414const _lib_path = path . join ( __dirname , '../spider/catvod' ) ;
1515
1616
17- const getRule = async function ( filePath , env ) {
18- const moduleObject = await init ( filePath , env ) ;
19- return JSON . stringify ( moduleObject ) ;
20- }
21-
2217const json2Object = function ( json ) {
2318 if ( ! json ) {
2419 return { }
@@ -28,6 +23,38 @@ const json2Object = function (json) {
2823 return JSON . parse ( json ) ;
2924}
3025
26+ const loadEsmWithHash = async function ( filePath , fileHash ) {
27+ const scriptUrl = `${ pathToFileURL ( filePath ) . href } ?v=${ fileHash } ` ;
28+ return await import ( scriptUrl ) ;
29+ }
30+
31+ const loadEsmWithEnv = async function ( filePath , env ) {
32+ const rawCode = await readFile ( filePath , 'utf8' ) ;
33+ let injectedCode = rawCode ;
34+ const esm_flag1 = 'export function __jsEvalReturn' ;
35+ const esm_flag2 = 'export default' ;
36+ const polyfill_code = 'var ENV={};\nvar getProxyUrl=null;\nexport const initEnv = (env)=>{ENV = env;if(env.getProxyUrl){getProxyUrl=env.getProxyUrl}};\n' ;
37+ if ( rawCode . includes ( esm_flag1 ) ) {
38+ injectedCode = rawCode . replace ( esm_flag1 , `${ polyfill_code } ${ esm_flag1 } ` )
39+ } else if ( rawCode . includes ( 'export default' ) ) {
40+ injectedCode = rawCode . replace ( esm_flag2 , `${ polyfill_code } ${ esm_flag2 } ` )
41+ }
42+ // console.log(injectedCode);
43+ // // 创建数据URI模块
44+ const dataUri = `data:text/javascript;base64,${ Buffer . from ( injectedCode ) . toString ( 'base64' ) } ` ;
45+ const module = await import ( dataUri ) ;
46+ const initEnv = module . initEnv ;
47+ if ( typeof initEnv === 'function' && env ) {
48+ initEnv ( env ) ;
49+ }
50+ return module
51+ }
52+
53+ const getRule = async function ( filePath , env ) {
54+ const moduleObject = await init ( filePath , env ) ;
55+ return JSON . stringify ( moduleObject ) ;
56+ }
57+
3158const init = async function ( filePath , env = { } , refresh ) {
3259 try {
3360 const fileContent = await readFile ( filePath , 'utf-8' ) ;
@@ -48,29 +75,30 @@ const init = async function (filePath, env = {}, refresh) {
4875 let hashMd5 = md5 ( filePath + '#pAq#' + moduleExt ) ;
4976 if ( moduleCache . has ( hashMd5 ) && ! refresh ) {
5077 const cached = moduleCache . get ( hashMd5 ) ;
51- if ( cached . hash === fileHash ) {
78+ // 除hash外还必须保证proxyUrl实时相等,避免本地代理url的尴尬情况
79+ if ( cached . hash === fileHash && cached . proxyUrl === env . proxyUrl ) {
5280 return cached . moduleObject ;
5381 }
5482 }
5583 log ( `Loading module: ${ filePath } ` ) ;
5684 let t1 = getNowTime ( ) ;
57- // const scriptUrl = pathToFileURL(filePath).href;
58- const scriptUrl = `${ pathToFileURL ( filePath ) . href } ?v=${ fileHash } ` ;
59- // console.log(scriptUrl);
60- const module = await import ( scriptUrl ) ;
85+ const module = await loadEsmWithEnv ( filePath , env ) ;
86+ // console.log('module:', module);
6187 let rule ;
6288 if ( module && module . __jsEvalReturn && typeof module . __jsEvalReturn === 'function' ) {
6389 rule = module . __jsEvalReturn ( ) ;
6490 } else {
6591 rule = module . default || module ;
6692 }
67- // console.log(rule);
93+ // console.log('rule:', rule);
94+ // console.log('globalThis.ENV:', globalThis.ENV);
95+ // console.log('globalThis.getProxyUrl:', globalThis.getProxyUrl);
6896 // 加载 init
6997 await rule . init ( moduleExt || { } ) ;
7098 let t2 = getNowTime ( ) ;
7199 const moduleObject = deepCopy ( rule ) ;
72100 moduleObject . cost = t2 - t1 ;
73- moduleCache . set ( hashMd5 , { moduleObject, hash : fileHash } ) ;
101+ moduleCache . set ( hashMd5 , { moduleObject, hash : fileHash , proxyUrl : env . proxyUrl } ) ;
74102 return moduleObject ;
75103 } catch ( error ) {
76104 console . log ( `Error in catvod.init :${ filePath } ` , error ) ;
0 commit comments