@@ -25,33 +25,56 @@ const json2Object = function (json) {
2525 return JSON . parse ( json ) ;
2626}
2727
28- const loadEsmWithHash = async function ( filePath , fileHash ) {
28+ const loadEsmWithHash = async function ( filePath , fileHash , env ) {
2929 const scriptUrl = `${ pathToFileURL ( filePath ) . href } ?v=${ fileHash } ` ;
30- return await import ( scriptUrl ) ;
30+ const module = await import ( scriptUrl ) ;
31+ const initEnv = module . initEnv ;
32+ if ( typeof initEnv === 'function' && env ) {
33+ initEnv ( env ) ;
34+ }
35+ return module
3136}
3237
3338const loadEsmWithEnv = async function ( filePath , env ) {
3439 const rawCode = await readFile ( filePath , 'utf8' ) ;
3540 let injectedCode = rawCode ;
41+ // let injectedCode = rawCode.replaceAll('assets://js/lib/', '../catLib/'); // esm-rregister处理了,这里不管
42+ // console.log('loadEsmWithEnv:', env);
3643 const esm_flag1 = 'export function __jsEvalReturn' ;
3744 const esm_flag2 = 'export default' ;
3845 const polyfill_code = `
3946var _ENV={};
4047var getProxyUrl=null;
4148var getProxy=null;
42- export const initEnv = (env)=> {
49+ export function initEnv(env){
4350 _ENV = env;
4451 if(env.getProxyUrl){
4552 getProxyUrl=env.getProxyUrl;
4653 getProxy=env.getProxyUrl
4754 }
48- }; ` . trim ( ) + '\n' ;
49- if ( rawCode . includes ( esm_flag1 ) ) {
50- injectedCode = rawCode . replace ( esm_flag1 , `${ polyfill_code } ${ esm_flag1 } ` )
51- } else if ( rawCode . includes ( 'export default' ) ) {
52- injectedCode = rawCode . replace ( esm_flag2 , `${ polyfill_code } ${ esm_flag2 } ` )
55+ }` . trim ( ) + '\n' ;
56+ if ( injectedCode . includes ( esm_flag1 ) ) {
57+ injectedCode = injectedCode . replace ( esm_flag1 , `${ polyfill_code } ${ esm_flag1 } ` )
58+ } else if ( injectedCode . includes ( 'export default' ) ) {
59+ injectedCode = injectedCode . replace ( esm_flag2 , `${ polyfill_code } ${ esm_flag2 } ` )
5360 }
54- // console.log(injectedCode);
61+
62+ // 改为在 esm-register.mjs 里实现,这里注释掉
63+ // if (injectedCode.includes('../catLib/crypto-js.js')) {
64+ // const cryptoJsPath = path.join(_lib_path, '../catLib', 'crypto-js.js');
65+ // // console.log('cryptoJsPath:',cryptoJsPath);
66+ // const cryptoHref = pathToFileURL(cryptoJsPath).href;
67+ // console.log('cryptoHref:', cryptoHref);
68+ // // const cryptoJsCode = await readFile(cryptoJsPath, 'utf-8');
69+ // // const cryptoJsBase64 = Buffer.from(cryptoJsCode).toString('base64');
70+ // injectedCode = injectedCode.replace(
71+ // '../catLib/crypto-js.js',
72+ // // `data:text/javascript;base64,${cryptoJsBase64}`
73+ // cryptoHref
74+ // );
75+ // }
76+
77+ // console.log('injectedCode:\n', injectedCode);
5578 // // 创建数据URI模块
5679 const dataUri = `data:text/javascript;base64,${ Buffer . from ( injectedCode ) . toString ( 'base64' ) } ` ;
5780 const module = await import ( dataUri ) ;
@@ -73,6 +96,12 @@ const init = async function (filePath, env = {}, refresh) {
7396 const fileHash = computeHash ( fileContent ) ;
7497 const moduleName = path . basename ( filePath , '.js' ) ;
7598 let moduleExt = env . ext || '' ;
99+ const default_init_cfg = {
100+ stype : 4 , //T3/T4 源类型
101+ skey : `catvod_${ moduleName } ` ,
102+ sourceKey : `catvod_${ moduleName } ` ,
103+ ext : moduleExt ,
104+ } ;
76105 let SitesMap = getSitesMap ( _config_path ) ;
77106 if ( moduleExt && SitesMap [ moduleName ] ) {
78107 try {
@@ -96,7 +125,7 @@ const init = async function (filePath, env = {}, refresh) {
96125 let t1 = getNowTime ( ) ;
97126 let module ;
98127 if ( enable_cat_debug ) {
99- module = await loadEsmWithHash ( filePath , fileHash ) ;
128+ module = await loadEsmWithHash ( filePath , fileHash , env ) ;
100129 } else {
101130 module = await loadEsmWithEnv ( filePath , env ) ;
102131 }
@@ -111,7 +140,7 @@ const init = async function (filePath, env = {}, refresh) {
111140 // console.log('globalThis.ENV:', globalThis.ENV);
112141 // console.log('globalThis.getProxyUrl:', globalThis.getProxyUrl);
113142 // 加载 init
114- await rule . init ( moduleExt || { } ) ;
143+ await rule . init ( default_init_cfg ) ;
115144 let t2 = getNowTime ( ) ;
116145 const moduleObject = deepCopy ( rule ) ;
117146 moduleObject . cost = t2 - t1 ;
@@ -130,7 +159,8 @@ const home = async function (filePath, env, filter = 1) {
130159
131160const homeVod = async function ( filePath , env ) {
132161 const moduleObject = await init ( filePath , env ) ;
133- return json2Object ( await moduleObject . homeVod ( ) ) ;
162+ const homeVodResult = json2Object ( await moduleObject . homeVod ( ) ) ;
163+ return homeVodResult && homeVodResult . list ? homeVodResult . list : homeVodResult ;
134164}
135165
136166
0 commit comments