@xuda.io/runtime-bundle 1.0.1222 → 1.0.1224
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
const _this={};export const init_module=async e=>{_this.func=e.func;_this.glb=e.glb;_this.SESSION_OBJ=e.SESSION_OBJ;_this.APP_OBJ=e.APP_OBJ;_this.IS_DOCKER=e.IS_DOCKER;_this.IS_API_SERVER=e.IS_API_SERVER;_this.IS_PROCESS_SERVER=e.IS_PROCESS_SERVER;if(!_this.xuda_dbs_plugin&&typeof _this.IS_DOCKER==="undefined"&&typeof _this.IS_PROCESS_SERVER==="undefined"&&typeof _this.IS_API_SERVER==="undefined"){await init_xuda_dbs_plugin(e.SESSION_ID)}_this.db_replication_store;_this.db_changes_store;_this.db_replication_from_stat=3;_this.db_replication_to_stat=3};export let _db={};_db.save_data=async function(SESSION_ID,dsSessionP,keyP){var db_driver=await this.get_db_driver(SESSION_ID,dsSessionP);if(db_driver==="pouchdb"&&_this.glb.IS_WORKER){var callback_id=_this.func.utils.set_callback_queue(SESSION_ID,callbackP);var obj=_this.func.utils.clean_returned_datasource(SESSION_ID,dsSessionP);return _this.func.utils.post_back_to_client(SESSION_ID,"execute_local_sava_data",_this.SESSION_OBJ[SESSION_ID].worker_id,{ds_obj:obj,dsSessionP:dsSessionP,keyP:keyP,callback_id:callback_id})}_db.save_indicator(SESSION_ID,true);var _ds=_this.SESSION_OBJ[SESSION_ID].DS_GLB[dsSessionP];let _view_obj=await _this.func.utils.VIEWS_OBJ.get(SESSION_ID,_ds.prog_id);var dataSourceTableId=_view_obj?.progDataSource?.dataSourceTableId;if(!dataSourceTableId){_db.save_indicator(SESSION_ID,false);_this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0105");return}var db_driver=await _db.get_db_driver(SESSION_ID,dsSessionP);let table_ret=await _this.func.utils.FILES_OBJ.get(SESSION_ID,dataSourceTableId);if(!table_ret.tableIndexes||_.isEmpty(table_ret.tableIndexes)){_db.save_indicator(SESSION_ID,false);_this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0104");return}if(!_ds.data_feed.rows){return console.error("data_feed rows is empty")}const clean_virtual_fields=async function(table_id,data){let table_obj=await func.utils.VIEWS_OBJ.get(SESSION_ID,table_id);var data_obj={};for(const[key,val]of Object.entries(data)){const _tableFieldsObj=func.common.find_item_by_key(table_obj.tableFields,"field_id",key);if(_tableFieldsObj){data_obj[key]=val}}return data_obj};let save_ret;if(_view_obj.properties.menuType==="component"){if(_view_obj?.properties?.rwMode!=="U"){return _this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0126")}let data={prog_id:_ds.prog_id};if(_ds.data_feed.rows_added?.length){if(!_view_obj?.properties?.allowCreate){_this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0120")}_.forEach(_ds.data_feed.rows_added,function(val,key){data[key]=val});delete _ds.data_feed.rows_added["newRecord"];save_ret=await _this.func.common.db(SESSION_ID,"dbs_create",data,{},dsSessionP);refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:save_ret.data.id,table_id:dataSourceTableId});return save_ret}if(_ds.data_feed.rows_deleted?.length){data.table_id=_ds._dataSourceTableId;data.ids=[];for await(const[key,val]of Object.entries(_ds.data_feed.rows_deleted)){data.ids.push(val);try{const row_idx=func.common.find_ROWID_idx(_ds,val);delete _ds.data_feed.rows[row_idx]}catch(error){}}_ds.data_feed.rows_deleted=[];save_ret=await _this.func.common.db(SESSION_ID,"dbs_delete",data,{},dsSessionP);refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:save_ret.data?.id?.[0],table_id:dataSourceTableId});return save_ret}if(_ds.data_feed.rows_changed?.length){for await(const[key,row_id]of Object.entries(_ds.data_feed.rows_changed)){if(row_id==="datasource_main")continue;let data={prog_id:_ds.prog_id};try{const row_idx=func.common.find_ROWID_idx(_ds,row_id);data.table_data=await clean_virtual_fields(_ds._dataSourceTableId,_ds.data_feed.rows[row_idx])}catch(err){console.error(err)}data.table_id=_ds._dataSourceTableId;data.row_id=row_id;save_ret=await _this.func.common.db(SESSION_ID,"dbs_update",data,{},dsSessionP);refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:save_ret.data?.id?.[0],table_id:dataSourceTableId})}return save_ret}return}if(_view_obj.properties.menuType!=="set_data"){return _this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0126")}var mode=_ds.set_mode;if(!mode||mode==="U"){let data={prog_id:_ds.prog_id};data.table_id=_ds._dataSourceTableId;data.row_id=_ds.currentRecordId;data.table_data={};try{const row_idx=func.common.find_ROWID_idx(_ds,_ds.currentRecordId);data.table_data=await clean_virtual_fields(_ds._dataSourceTableId,_ds.data_feed.rows[row_idx])}catch(err){console.error(err)}save_ret=await _this.func.common.db(SESSION_ID,"dbs_update",data,{},dsSessionP);refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:save_ret.data?.id?.[0],table_id:dataSourceTableId})}if(mode==="D"){let data={prog_id:_ds.prog_id};data.table_id=_ds._dataSourceTableId;data.ids=[_ds.currentRecordId];save_ret=await _this.func.common.db(SESSION_ID,"dbs_delete",data,{},dsSessionP);refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:save_ret.data?.id?.[0],table_id:dataSourceTableId});return save_ret}if(mode==="C"){if(_view_obj.properties.crudMode!=="C"&&_view_obj.properties.crudMode==="U"&&!_view_obj.properties.allowCreate){return _this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0120")}let data={prog_id:_ds.prog_id};data.table_id=_ds._dataSourceTableId;try{const row_idx=func.common.find_ROWID_idx(_ds,_ds.currentRecordId);data.table_data=_ds.data_feed.rows[row_idx];save_ret=await _this.func.common.db(SESSION_ID,"dbs_create",data,{},dsSessionP);refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:save_ret.data.id,table_id:dataSourceTableId});return save_ret}catch(err){console.error(err)}}};_db.get_db_driver=async function(SESSION_ID,dsSessionP){var db_driver;if(dsSessionP){var _ds=_this.SESSION_OBJ[SESSION_ID].DS_GLB[dsSessionP];let _view_obj=await _this.func.utils.VIEWS_OBJ.get(SESSION_ID,_ds.prog_id);var dataSourceTableId=_view_obj?.progDataSource?.dataSourceTableId;if(!dataSourceTableId){_this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0105");return}let file_ret=await _this.func.utils.FILES_OBJ.get(SESSION_ID,dataSourceTableId);db_driver=file_ret.properties.db_driver||"xuda"}if(_this.SESSION_OBJ[SESSION_ID].engine_mode==="live_preview"&&!db_driver&&_this.SESSION_OBJ[SESSION_ID].app_id==="temp"){db_driver="pouchdb"}return db_driver};_db.get_query=async function(SESSION_ID,table_id,queryP,dsSessionP,viewSourceDescP,sourceP,reduceP,skipP,limitP,countP,idsP,sortModel,sort_dir,filterModel={filterModelNative:filterModelNative,filterModelMongo:filterModelMongo,filterModelSql:filterModelSql,filterModelUserMongo:filterModelUserMongo,filterModelUserSql:filterModelUserSql},dataSourceFilterModelType="index",total_fields_info){const get_fields=async function(_ds){var fields=[];const _view_obj=await _this.func.utils.VIEWS_OBJ.get(SESSION_ID,_ds.prog_id);if(_view_obj.progFields){for(let val of _view_obj.progFields){if(val.data.type==="table"){fields.push(val.data.field_id)}}}return fields};const run_local=async function(){if(!_this.SESSION_OBJ[SESSION_ID])return;if(!_this.SESSION_OBJ[SESSION_ID].DS_GLB[dsSessionP])return;var ds=_this.SESSION_OBJ[SESSION_ID].DS_GLB[dsSessionP];var v=ds.v;var query_string;if(queryP.indexId){if(countP&&queryP.desc){query_string=`startkey=${JSON.stringify(queryP.endkey)}&endkey=${JSON.stringify(queryP.startkey)}`}else{query_string=`startkey=${JSON.stringify(queryP.startkey)}&endkey=${JSON.stringify(queryP.endkey)}&desc=${queryP.desc}`}}else{query_string="key="+queryP.table_id}var db_driver=await _db.get_db_driver(SESSION_ID,dsSessionP);var fields_arr=await get_fields(ds);const response={success:async function(json){if(dsSessionP&&!_this.SESSION_OBJ[SESSION_ID]?.DS_GLB?.[dsSessionP]){return}if(json.code<0){response.error(json.data);return}var data=json.data;if(dsSessionP)_this.func.utils.debug.log(SESSION_ID,ds._dataSourceTableId,{module:ds.viewModule,action:"read",prop:sourceP,details:query_string,result:data.rows?data.rows.length:0,error:null,source:viewSourceDescP,json:data,fields:_.assignIn(ds.locate_from,ds.locate_to),type:"file",dsSession:dsSessionP});return data},error:async function(err){if(dsSessionP&&!ds)return;if(dsSessionP){var err_msg=err&&err.data?err.data:err;_this.func.utils.debug.log(SESSION_ID,_this.SESSION_OBJ[SESSION_ID].DS_GLB[dsSessionP]._dataSourceTableId,{module:ds.viewModule,action:"read",prop:sourceP,details:query_string,result:null,error:err_msg,source:viewSourceDescP,json:null,fields:_.assignIn(ds.locate_from,ds.locate_to),type:"file",dsSession:dsSessionP})}_this.func.utils.debug_report(SESSION_ID,"datasource couchDB query "+sourceP,err_msg,"E");return{rows:[],error:true}}};var data={desc:queryP.desc,indexId:queryP.indexId,key:queryP.table_id,reduce:reduceP,viewSourceDesc:viewSourceDescP,limit:limitP,skip:skipP,count:countP,ids:idsP,table_id:queryP.table_id,prog_id:ds.prog_id};data.filter_from=JSON.stringify(queryP.filter_from);data.filter_to=JSON.stringify(queryP.filter_to);data.sortOrder=queryP.sortOrder;data.table_id=queryP.table_id;data.startkey=JSON.stringify(queryP.startkey);data.endkey=JSON.stringify(queryP.endkey);if(countP&&queryP.desc){data.sortOrder="asc";data.startkey=JSON.stringify(queryP.endkey);data.endkey=JSON.stringify(queryP.startkey)}data.dataSourceFilterModelType=dataSourceFilterModelType;if(sortModel){data.sortModel=JSON.stringify(sortModel);data.sort_dir=sort_dir}if(!_.isEmpty(filterModel?.filterModelNative)){data.filterModelNative=JSON.stringify(filterModel.filterModelNative)}if(!_.isEmpty(filterModel?.filterModelMongo)){data.filterModelMongo=JSON.stringify(filterModel.filterModelMongo)}if(filterModel?.filterModelSql){data.filterModelSql=JSON.stringify(filterModel.filterModelSql)}if(!_.isEmpty(filterModel?.filterModelUserMongo)){data.filterModelUserMongo=JSON.stringify(filterModel.filterModelUserMongo)}if(filterModel?.filterModelUserSql){data.filterModelUserSql=JSON.stringify(filterModel.filterModelUserSql)}if(total_fields_info){data.total_fields_info=JSON.stringify(total_fields_info)}if(!reduceP&&!countP){data.fields=fields_arr}try{const json=await _this.func.common.db(SESSION_ID,"dbs_read",data,{node:true},dsSessionP);return await response.success(json)}catch(e){return await response.error(e?.message||e?.data||e)}};var db_driver=await this.get_db_driver(SESSION_ID,dsSessionP);if(db_driver==="pouchdb"&&_this.glb.IS_WORKER&&!_this.SESSION_OBJ[SESSION_ID].engine_mode==="live_preview"){var callback_id=_this.func.utils.set_callback_queue(SESSION_ID,callbackP);var obj=_this.func.utils.clean_returned_datasource(SESSION_ID,dsSessionP);return _this.func.utils.post_back_to_client(SESSION_ID,"execute_local_db_query",_this.SESSION_OBJ[SESSION_ID].worker_id,{ds_obj:obj,dsSessionP:dsSessionP,table_id:table_id,queryP:queryP,dsSessionP:dsSessionP,viewSourceDescP:viewSourceDescP,sourceP:sourceP,reduceP:reduceP,skipP:skipP,limitP:limitP,countP:countP,idsP:idsP,callback_id:callback_id})}return await run_local()};_db.utils={};_db.save_indicator=function(SESSION_ID,stateP){if(!_this.glb.IS_WORKER){_this.func.UI.utils.save(SESSION_ID,stateP)}};const init_xuda_dbs_plugin=async function(SESSION_ID){var _session=_this.SESSION_OBJ[SESSION_ID];const plugin_name="@xuda.io/xuda-dbs-plugin-xuda";const get_path=function(plugin_name,resource){var path=`https://${_session.domain}/plugins/${plugin_name}${resource?"/"+resource:""}?app_id=${_session.app_id}`;return path};try{_this.xuda_dbs_plugin=await import(get_path(plugin_name,"studio.mjs"));const db=await func.utils.connect_pouchdb(SESSION_ID);await _db.call_pouch_xuda_dbs_plugin(SESSION_ID,"create_design",{db:db})}catch(err){return console.error(err.message)}};_db.call_pouch_xuda_dbs_plugin=async function(SESSION_ID,method,params){return new Promise(async(resolve,reject)=>{try{if(params.e){params.table_obj=await func.utils.DOCS_OBJ.get(SESSION_ID,params.e.table_id)}await _this.xuda_dbs_plugin[method](params,resolve,reject)}catch(err){reject()}})};_db.pouch={};_db.pouch.dbs_read=async function(SESSION_ID,e){const db=await func.utils.connect_pouchdb(SESSION_ID);return await _db.call_pouch_xuda_dbs_plugin(SESSION_ID,"read",{db:db,e:e})};_db.pouch.dbs_create=async function(SESSION_ID,e){const db=await func.utils.connect_pouchdb(SESSION_ID);return await _db.call_pouch_xuda_dbs_plugin(SESSION_ID,"create",{db:db,e:e})};_db.pouch.dbs_delete=async function(SESSION_ID,e){const db=await func.utils.connect_pouchdb(SESSION_ID);return await _db.call_pouch_xuda_dbs_plugin(SESSION_ID,"del",{db:db,e:e})};_db.pouch.dbs_update=async function(SESSION_ID,e){const db=await func.utils.connect_pouchdb(SESSION_ID);return await _db.call_pouch_xuda_dbs_plugin(SESSION_ID,"update",{db:db,e:e})};_db.pouch.init_db_replication=async function(SESSION_ID){const db=await func.utils.connect_pouchdb(SESSION_ID);var _session=_this.SESSION_OBJ[SESSION_ID];if(_session.url_params.avoid_local||_session.engine_mode==="miniapp"||_session.engine_mode!=="live_preview"&&!_session.opt.enable_offline){return}const changes=db.changes({live:true,include_docs:true}).on("change",function(change){if(!change.doc.udfData)return;func.UI.screen.refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:change.id,table_id:change.doc.udfData.udffileid})}).on("complete",function(info){console.log("complete",info)}).on("error",function(err){console.error(err)});set_replication_stat(SESSION_ID,null,3);const db_name=APP_OBJ[_session.app_id].app_db_name;const rep=db.replicate.to(`https://${_session.domain}/db/${db_name}`,{selector:{docType:"database"},live:true,retry:true,fetch:function(url,opts){if(_session.gtp_token){opts.headers.set("xu-gtp-token",_session.gtp_token)}if(_session.app_token)opts.headers.set("xu-app-token",_session.app_token);{opts.headers.set("xu-db",db_name)}return PouchDB.fetch(url,opts)}}).on("change",function(info){}).on("paused",function(err){if(err){return set_replication_stat(SESSION_ID,null,2)}set_replication_stat(SESSION_ID,null,3)}).on("active",function(){}).on("denied",function(err){set_replication_stat(SESSION_ID,null,2)}).on("complete",function(info){}).on("error",function(err){set_replication_stat(SESSION_ID,null,2)});_db.pouch.set_db_replication_from_server(SESSION_ID)};_db.pouch.set_db_replication_from_server=async function(SESSION_ID){const db=await func.utils.connect_pouchdb(SESSION_ID);const rep_docs=await _db.pouch.get_replications(SESSION_ID);const _session=_this.SESSION_OBJ[SESSION_ID];const db_name=APP_OBJ[_session.app_id].app_db_name;const remote_db_url=`https://${_session.domain}/db/${db_name}`;const remote_db=new PouchDB(remote_db_url,{fetch:function(url,opts){if(_session.gtp_token){opts.headers.set("xu-gtp-token",_session.gtp_token)}if(_session.app_token)opts.headers.set("xu-app-token",_session.app_token);{opts.headers.set("xu-db",db_name)}return PouchDB.fetch(url,opts)}});if(_this.db_changes_store){_this.db_changes_store.cancel()}const write_stat=async function(rep_id,stat,reason){try{let doc=await db.get(rep_id);doc.stat=stat;doc.stat_reason=reason;try{db.put(doc)}catch(err){}}catch(err){}};const download_documents=async function(rep_doc){await write_stat(rep_doc._id,2);const remote_db=new PouchDB(remote_db_url,{fetch:function(url,opts){opts.headers.set("xu-prog_id",rep_doc.prog_id);if(_session.gtp_token){opts.headers.set("xu-gtp-token",_session.gtp_token)}if(_session.app_token)opts.headers.set("xu-app-token",_session.app_token);{opts.headers.set("xu-db",db_name)}return PouchDB.fetch(url,opts)}});const ret=await remote_db.find({selector:rep_doc.selector,fields:["_id"],limit:9999999999});if(ret?.code<0){return func.utils.debug_report(SESSION_ID,"download_documents",ret.data,"E")}try{var doc_ids=[];for await(let doc of ret.docs){doc_ids.push(doc._id)}db.replicate.from(remote_db_url,{doc_ids:doc_ids,retry:true,style:"main_only",fetch:function(url,opts){if(_session.gtp_token){opts.headers.set("xu-gtp-token",_session.gtp_token)}if(_session.app_token)opts.headers.set("xu-app-token",_session.app_token);{opts.headers.set("xu-db",db_name)}return PouchDB.fetch(url,opts)}}).on("paused",async function(err){if(err){return}const read_ret=await _db.pouch.dbs_read(SESSION_ID,rep_doc.e);await write_stat(rep_doc._id,3)})}catch(err){await write_stat(rep_doc._id,2,err.message);console.error("replication from error",err)}};let filter_tables=[];for(let doc of rep_docs){if(!filter_tables.includes(doc.table_id))filter_tables.push(doc.table_id);if(doc.stat<3)await download_documents(doc)}try{_this.db_changes_store=remote_db.changes({since:"now",live:true,timeout:3e5,style:"main_only",include_docs:false}).on("change",async function(change){if(change.id.substr(0,4)!=="dbs-")return;if(_this.db_replication_from_stat!==3){set_replication_stat(SESSION_ID,3)}try{const existing_doc=await db.get(change.id);db.replicate.from(remote_db_url,{doc_ids:[change.id],retry:true,style:"main_only",fetch:function(url,opts){if(_session.gtp_token){opts.headers.set("xu-gtp-token",_session.gtp_token)}if(_session.app_token)opts.headers.set("xu-app-token",_session.app_token);{opts.headers.set("xu-db",db_name)}return PouchDB.fetch(url,opts)}})}catch(err){try{const table_id=change.id.split("_")[0].substr(4);for(let doc of rep_docs){if(doc.table_id===table_id){download_documents(doc)}}}catch(err){console.warn(err)}}}).on("complete",function(info){}).on("error",function(err){console.error(err);set_replication_stat(SESSION_ID,2);_this.db_changes_store.cancel()})}catch(err){console.error(err)}return};_db.pouch.remove_db_replication_from_server=async function(SESSION_ID,rep_id){const db=await func.utils.connect_pouchdb(SESSION_ID);try{const doc=await db.get(rep_id);db.remove(doc)}catch(error){}};_db.pouch.get_replications=async function(SESSION_ID=Object.keys(SESSION_OBJ)[0]){const db=await func.utils.connect_pouchdb(SESSION_ID);let ret=await db.find({selector:{docType:"rep_request"}});return ret.docs};_db.pouch.get_replication_stat=async function(SESSION_ID){const _session=_this.SESSION_OBJ[SESSION_ID];return _session?.DS_GLB?.[0]?.data_system?.SYS_GLOBAL_BOL_REPLICATION_STAT};const set_replication_stat=async function(SESSION_ID,from_stat,to_stat){var _session=_this.SESSION_OBJ[SESSION_ID];if(!_session?.DS_GLB?.[0]){return setTimeout(function(){set_replication_stat(SESSION_ID,from_stat,to_stat)},1e3)}if(from_stat)_this.db_replication_from_stat=from_stat;if(to_stat)_this.db_replication_to_stat=to_stat;var data={};if(_this.db_replication_from_stat===3&&_this.db_replication_to_stat===3){data.SYS_GLOBAL_BOL_REPLICATION_STAT=1;console.info("Replication On")}else{data.SYS_GLOBAL_BOL_REPLICATION_STAT=0;console.info("Replication Off")}var datasource_changes={[0]:{["data_system"]:data}};await func.datasource.update(SESSION_ID,datasource_changes)};const refresh_document_changes_for_realtime_update=function(SESSION_ID,doc_change){var _session=_this.SESSION_OBJ[SESSION_ID];if(glb.IS_WORKER){return func.utils.post_back_to_client(SESSION_ID,"refresh_document_changes_for_realtime_update",_session.worker_id,{doc_change:doc_change})}func.UI.screen.refresh_document_changes_for_realtime_update(SESSION_ID,doc_change)};
|
|
1
|
+
const _this={};export const init_module=async e=>{_this.func=e.func;_this.glb=e.glb;_this.SESSION_OBJ=e.SESSION_OBJ;_this.APP_OBJ=e.APP_OBJ;_this.IS_DOCKER=e.IS_DOCKER;_this.IS_API_SERVER=e.IS_API_SERVER;_this.IS_PROCESS_SERVER=e.IS_PROCESS_SERVER;if(!_this.xuda_dbs_plugin&&typeof _this.IS_DOCKER==="undefined"&&typeof _this.IS_PROCESS_SERVER==="undefined"&&typeof _this.IS_API_SERVER==="undefined"){await init_xuda_dbs_plugin(e.SESSION_ID)}_this.db_replication_store;_this.db_changes_store;_this.db_replication_from_stat=3;_this.db_replication_to_stat=3};export let _db={};_db.save_data=async function(SESSION_ID,dsSessionP,keyP){var db_driver=await this.get_db_driver(SESSION_ID,dsSessionP);if(db_driver==="pouchdb"&&_this.glb.IS_WORKER){var callback_id=_this.func.utils.set_callback_queue(SESSION_ID,callbackP);var obj=_this.func.utils.clean_returned_datasource(SESSION_ID,dsSessionP);return _this.func.utils.post_back_to_client(SESSION_ID,"execute_local_sava_data",_this.SESSION_OBJ[SESSION_ID].worker_id,{ds_obj:obj,dsSessionP:dsSessionP,keyP:keyP,callback_id:callback_id})}_db.save_indicator(SESSION_ID,true);var _ds=_this.SESSION_OBJ[SESSION_ID].DS_GLB[dsSessionP];let _view_obj=await _this.func.utils.VIEWS_OBJ.get(SESSION_ID,_ds.prog_id);var dataSourceTableId=_view_obj?.progDataSource?.dataSourceTableId;if(!dataSourceTableId){_db.save_indicator(SESSION_ID,false);_this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0105");return}var db_driver=await _db.get_db_driver(SESSION_ID,dsSessionP);let table_ret=await _this.func.utils.FILES_OBJ.get(SESSION_ID,dataSourceTableId);if(!table_ret.tableIndexes||_.isEmpty(table_ret.tableIndexes)){_db.save_indicator(SESSION_ID,false);_this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0104");return}if(!_ds.data_feed.rows){return console.error("data_feed rows is empty")}const clean_virtual_fields=async function(table_id,data){let table_obj=await func.utils.VIEWS_OBJ.get(SESSION_ID,table_id);var data_obj={};for(const[key,val]of Object.entries(data)){const _tableFieldsObj=func.common.find_item_by_key(table_obj.tableFields,"field_id",key);if(_tableFieldsObj){data_obj[key]=val}}return data_obj};let save_ret;if(_view_obj.properties.menuType==="component"){if(_view_obj?.properties?.rwMode!=="U"){return _this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0126")}let data={prog_id:_ds.prog_id};if(_ds.data_feed.rows_added?.length){if(!_view_obj?.properties?.allowCreate){_this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0120")}_.forEach(_ds.data_feed.rows_added,function(val,key){data[key]=val});delete _ds.data_feed.rows_added["newRecord"];save_ret=await _this.func.common.db(SESSION_ID,"dbs_create",data,{},dsSessionP);refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:save_ret.data.id,table_id:dataSourceTableId});return save_ret}if(_ds.data_feed.rows_deleted?.length){data.table_id=_ds._dataSourceTableId;data.ids=[];for await(const[key,val]of Object.entries(_ds.data_feed.rows_deleted)){data.ids.push(val);try{const row_idx=func.common.find_ROWID_idx(_ds,val);delete _ds.data_feed.rows[row_idx]}catch(error){}}_ds.data_feed.rows_deleted=[];save_ret=await _this.func.common.db(SESSION_ID,"dbs_delete",data,{},dsSessionP);refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:save_ret.data?.id?.[0],table_id:dataSourceTableId});return save_ret}if(_ds.data_feed.rows_changed?.length){for await(const[key,row_id]of Object.entries(_ds.data_feed.rows_changed)){if(row_id==="datasource_main")continue;let data={prog_id:_ds.prog_id};try{const row_idx=func.common.find_ROWID_idx(_ds,row_id);data.table_data=await clean_virtual_fields(_ds._dataSourceTableId,_ds.data_feed.rows[row_idx])}catch(err){console.error(err)}data.table_id=_ds._dataSourceTableId;data.row_id=row_id;save_ret=await _this.func.common.db(SESSION_ID,"dbs_update",data,{},dsSessionP);refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:save_ret.data?.id?.[0],table_id:dataSourceTableId})}return save_ret}return}if(_view_obj.properties.menuType!=="set_data"){return _this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0126")}var mode=_ds.set_mode;if(!mode||mode==="U"){let data={prog_id:_ds.prog_id};data.table_id=_ds._dataSourceTableId;data.row_id=_ds.currentRecordId;data.table_data={};try{const row_idx=func.common.find_ROWID_idx(_ds,_ds.currentRecordId);data.table_data=await clean_virtual_fields(_ds._dataSourceTableId,_ds.data_feed.rows[row_idx])}catch(err){console.error(err)}save_ret=await _this.func.common.db(SESSION_ID,"dbs_update",data,{},dsSessionP);refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:save_ret.data?.id?.[0],table_id:dataSourceTableId})}if(mode==="D"){let data={prog_id:_ds.prog_id};data.table_id=_ds._dataSourceTableId;data.ids=[_ds.currentRecordId];save_ret=await _this.func.common.db(SESSION_ID,"dbs_delete",data,{},dsSessionP);refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:save_ret.data?.id?.[0],table_id:dataSourceTableId});return save_ret}if(mode==="C"){if(_view_obj.properties.crudMode!=="C"&&_view_obj.properties.crudMode==="U"&&!_view_obj.properties.allowCreate){return _this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0120")}let data={prog_id:_ds.prog_id};data.table_id=_ds._dataSourceTableId;try{const row_idx=func.common.find_ROWID_idx(_ds,_ds.currentRecordId);data.table_data=_ds.data_feed.rows[row_idx];save_ret=await _this.func.common.db(SESSION_ID,"dbs_create",data,{},dsSessionP);refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:save_ret.data.id,table_id:dataSourceTableId});return save_ret}catch(err){console.error(err)}}};_db.get_db_driver=async function(SESSION_ID,dsSessionP){var db_driver;if(dsSessionP){var _ds=_this.SESSION_OBJ[SESSION_ID].DS_GLB[dsSessionP];let _view_obj=await _this.func.utils.VIEWS_OBJ.get(SESSION_ID,_ds.prog_id);var dataSourceTableId=_view_obj?.progDataSource?.dataSourceTableId;if(!dataSourceTableId){_this.func.utils.alerts.invoke(SESSION_ID,"system_msg","SYS_MSG_0105");return}let file_ret=await _this.func.utils.FILES_OBJ.get(SESSION_ID,dataSourceTableId);db_driver=file_ret.properties.db_driver||"xuda"}if(_this.SESSION_OBJ[SESSION_ID].engine_mode==="live_preview"&&!db_driver&&_this.SESSION_OBJ[SESSION_ID].app_id==="temp"){db_driver="pouchdb"}return db_driver};_db.get_query=async function(SESSION_ID,table_id,queryP,dsSessionP,viewSourceDescP,sourceP,reduceP,skipP,limitP,countP,idsP,sortModel,sort_dir,filterModel={filterModelNative:filterModelNative,filterModelMongo:filterModelMongo,filterModelSql:filterModelSql,filterModelUserMongo:filterModelUserMongo,filterModelUserSql:filterModelUserSql},dataSourceFilterModelType="index",total_fields_info){const get_fields=async function(_ds){var fields=[];const _view_obj=await _this.func.utils.VIEWS_OBJ.get(SESSION_ID,_ds.prog_id);if(_view_obj.progFields){for(let val of _view_obj.progFields){if(val.data.type==="table"){fields.push(val.data.field_id)}}}return fields};const run_local=async function(){if(!_this.SESSION_OBJ[SESSION_ID])return;if(!_this.SESSION_OBJ[SESSION_ID].DS_GLB[dsSessionP])return;var ds=_this.SESSION_OBJ[SESSION_ID].DS_GLB[dsSessionP];var v=ds.v;var query_string;if(queryP.indexId){if(countP&&queryP.desc){query_string=`startkey=${JSON.stringify(queryP.endkey)}&endkey=${JSON.stringify(queryP.startkey)}`}else{query_string=`startkey=${JSON.stringify(queryP.startkey)}&endkey=${JSON.stringify(queryP.endkey)}&desc=${queryP.desc}`}}else{query_string="key="+queryP.table_id}var db_driver=await _db.get_db_driver(SESSION_ID,dsSessionP);var fields_arr=await get_fields(ds);const response={success:async function(json){if(dsSessionP&&!_this.SESSION_OBJ[SESSION_ID]?.DS_GLB?.[dsSessionP]){return}if(json.code<0){response.error(json.data);return}var data=json.data;if(dsSessionP)_this.func.utils.debug.log(SESSION_ID,ds._dataSourceTableId,{module:ds.viewModule,action:"read",prop:sourceP,details:query_string,result:data.rows?data.rows.length:0,error:null,source:viewSourceDescP,json:data,fields:_.assignIn(ds.locate_from,ds.locate_to),type:"file",dsSession:dsSessionP});return data},error:async function(err){if(dsSessionP&&!ds)return;if(dsSessionP){var err_msg=err&&err.data?err.data:err;_this.func.utils.debug.log(SESSION_ID,_this.SESSION_OBJ[SESSION_ID].DS_GLB[dsSessionP]._dataSourceTableId,{module:ds.viewModule,action:"read",prop:sourceP,details:query_string,result:null,error:err_msg,source:viewSourceDescP,json:null,fields:_.assignIn(ds.locate_from,ds.locate_to),type:"file",dsSession:dsSessionP})}_this.func.utils.debug_report(SESSION_ID,"datasource couchDB query "+sourceP,err_msg,"E");return{rows:[],error:true}}};var data={desc:queryP.desc,indexId:queryP.indexId,key:queryP.table_id,reduce:reduceP,viewSourceDesc:viewSourceDescP,limit:limitP,skip:skipP,count:countP,ids:idsP,table_id:queryP.table_id,prog_id:ds.prog_id};data.filter_from=JSON.stringify(queryP.filter_from);data.filter_to=JSON.stringify(queryP.filter_to);data.sortOrder=queryP.sortOrder;data.table_id=queryP.table_id;data.startkey=JSON.stringify(queryP.startkey);data.endkey=JSON.stringify(queryP.endkey);if(countP&&queryP.desc){data.sortOrder="asc";data.startkey=JSON.stringify(queryP.endkey);data.endkey=JSON.stringify(queryP.startkey)}data.dataSourceFilterModelType=dataSourceFilterModelType;if(sortModel){data.sortModel=JSON.stringify(sortModel);data.sort_dir=sort_dir}if(!_.isEmpty(filterModel?.filterModelNative)){data.filterModelNative=JSON.stringify(filterModel.filterModelNative)}if(!_.isEmpty(filterModel?.filterModelMongo)){data.filterModelMongo=JSON.stringify(filterModel.filterModelMongo)}if(filterModel?.filterModelSql){data.filterModelSql=JSON.stringify(filterModel.filterModelSql)}if(!_.isEmpty(filterModel?.filterModelUserMongo)){data.filterModelUserMongo=JSON.stringify(filterModel.filterModelUserMongo)}if(filterModel?.filterModelUserSql){data.filterModelUserSql=JSON.stringify(filterModel.filterModelUserSql)}if(total_fields_info){data.total_fields_info=JSON.stringify(total_fields_info)}if(!reduceP&&!countP){data.fields=fields_arr}try{const json=await _this.func.common.db(SESSION_ID,"dbs_read",data,{node:true},dsSessionP);return await response.success(json)}catch(e){return await response.error(e?.message||e?.data||e)}};var db_driver=await this.get_db_driver(SESSION_ID,dsSessionP);if(db_driver==="pouchdb"&&_this.glb.IS_WORKER&&!_this.SESSION_OBJ[SESSION_ID].engine_mode==="live_preview"){var callback_id=_this.func.utils.set_callback_queue(SESSION_ID,callbackP);var obj=_this.func.utils.clean_returned_datasource(SESSION_ID,dsSessionP);return _this.func.utils.post_back_to_client(SESSION_ID,"execute_local_db_query",_this.SESSION_OBJ[SESSION_ID].worker_id,{ds_obj:obj,dsSessionP:dsSessionP,table_id:table_id,queryP:queryP,dsSessionP:dsSessionP,viewSourceDescP:viewSourceDescP,sourceP:sourceP,reduceP:reduceP,skipP:skipP,limitP:limitP,countP:countP,idsP:idsP,callback_id:callback_id})}return await run_local()};_db.utils={};_db.save_indicator=function(SESSION_ID,stateP){if(!_this.glb.IS_WORKER){_this.func.UI.utils.save(SESSION_ID,stateP)}};const init_xuda_dbs_plugin=async function(SESSION_ID){var _session=_this.SESSION_OBJ[SESSION_ID];const plugin_name="@xuda.io/xuda-dbs-plugin-xuda";const get_path=function(plugin_name,resource){var path=`https://${_session.domain}/plugins/${plugin_name}${resource?"/"+resource:""}?app_id=${_session.app_id}`;return path};try{_this.xuda_dbs_plugin=await import(get_path(plugin_name,"studio.mjs"));const db=await func.utils.connect_pouchdb(SESSION_ID);await _db.call_pouch_xuda_dbs_plugin(SESSION_ID,"create_design",{db:db})}catch(err){return console.error(err.message)}};_db.call_pouch_xuda_dbs_plugin=async function(SESSION_ID,method,params){return new Promise(async(resolve,reject)=>{try{if(params.e){params.table_obj=await func.utils.DOCS_OBJ.get(SESSION_ID,params.e.table_id)}await _this.xuda_dbs_plugin[method](params,resolve,reject)}catch(err){reject()}})};_db.pouch={};_db.pouch.dbs_read=async function(SESSION_ID,e){const db=await func.utils.connect_pouchdb(SESSION_ID);return await _db.call_pouch_xuda_dbs_plugin(SESSION_ID,"read",{db:db,e:e})};_db.pouch.dbs_create=async function(SESSION_ID,e){const db=await func.utils.connect_pouchdb(SESSION_ID);return await _db.call_pouch_xuda_dbs_plugin(SESSION_ID,"create",{db:db,e:e})};_db.pouch.dbs_delete=async function(SESSION_ID,e){const db=await func.utils.connect_pouchdb(SESSION_ID);return await _db.call_pouch_xuda_dbs_plugin(SESSION_ID,"del",{db:db,e:e})};_db.pouch.dbs_update=async function(SESSION_ID,e){const db=await func.utils.connect_pouchdb(SESSION_ID);return await _db.call_pouch_xuda_dbs_plugin(SESSION_ID,"update",{db:db,e:e})};_db.pouch.init_db_replication=async function(SESSION_ID){const db=await func.utils.connect_pouchdb(SESSION_ID);var _session=_this.SESSION_OBJ[SESSION_ID];if(_session.url_params.avoid_local||_session.engine_mode==="miniapp"||_session.engine_mode!=="live_preview"&&!_session.opt.enable_offline){return}const changes=db.changes({live:true,include_docs:true}).on("change",function(change){if(!change.doc.udfData)return;func.UI.screen.refresh_document_changes_for_realtime_update(SESSION_ID,{row_id:change.id,table_id:change.doc.udfData.udffileid})}).on("complete",function(info){console.log("complete",info)}).on("error",function(err){console.error(err)});set_replication_stat(SESSION_ID,null,3);const db_name=APP_OBJ[_session.app_id].app_db_name;const rep=db.replicate.to(`https://${_session.domain}/db/${db_name}`,{selector:{docType:"database"},live:true,retry:true,fetch:function(url,opts){if(_session.gtp_token){opts.headers.set("xu-gtp-token",_session.gtp_token)}if(_session.app_token)opts.headers.set("xu-app-token",_session.app_token);{opts.headers.set("xu-db",db_name)}return PouchDB.fetch(url,opts)}}).on("change",function(info){}).on("paused",function(err){if(err){return set_replication_stat(SESSION_ID,null,2)}set_replication_stat(SESSION_ID,null,3)}).on("active",function(){}).on("denied",function(err){set_replication_stat(SESSION_ID,null,2)}).on("complete",function(info){}).on("error",function(err){set_replication_stat(SESSION_ID,null,2)});_db.pouch.set_db_replication_from_server(SESSION_ID)};_db.pouch.set_db_replication_from_server=async function(SESSION_ID){if(!navigator.onLine)return;const db=await func.utils.connect_pouchdb(SESSION_ID);const rep_docs=await _db.pouch.get_replications(SESSION_ID);const _session=_this.SESSION_OBJ[SESSION_ID];const db_name=APP_OBJ[_session.app_id].app_db_name;const remote_db_url=`https://${_session.domain}/db/${db_name}`;const remote_db=new PouchDB(remote_db_url,{fetch:function(url,opts){if(_session.gtp_token){opts.headers.set("xu-gtp-token",_session.gtp_token)}if(_session.app_token)opts.headers.set("xu-app-token",_session.app_token);{opts.headers.set("xu-db",db_name)}return PouchDB.fetch(url,opts)}});if(_this.db_changes_store){_this.db_changes_store.cancel()}const write_stat=async function(rep_id,stat,reason){try{let doc=await db.get(rep_id);doc.stat=stat;doc.stat_reason=reason;try{db.put(doc)}catch(err){}}catch(err){}};const download_documents=async function(rep_doc){await write_stat(rep_doc._id,2);const remote_db=new PouchDB(remote_db_url,{fetch:function(url,opts){opts.headers.set("xu-prog_id",rep_doc.prog_id);if(_session.gtp_token){opts.headers.set("xu-gtp-token",_session.gtp_token)}if(_session.app_token)opts.headers.set("xu-app-token",_session.app_token);{opts.headers.set("xu-db",db_name)}return PouchDB.fetch(url,opts)}});const ret=await remote_db.find({selector:rep_doc.selector,fields:["_id"],limit:9999999999});if(ret?.code<0){return func.utils.debug_report(SESSION_ID,"download_documents",ret.data,"E")}try{var doc_ids=[];for await(let doc of ret.docs){doc_ids.push(doc._id)}db.replicate.from(remote_db_url,{doc_ids:doc_ids,retry:true,style:"main_only",fetch:function(url,opts){if(_session.gtp_token){opts.headers.set("xu-gtp-token",_session.gtp_token)}if(_session.app_token)opts.headers.set("xu-app-token",_session.app_token);{opts.headers.set("xu-db",db_name)}return PouchDB.fetch(url,opts)}}).on("paused",async function(err){if(err){return}const read_ret=await _db.pouch.dbs_read(SESSION_ID,rep_doc.e);await write_stat(rep_doc._id,3)})}catch(err){await write_stat(rep_doc._id,2,err.message);console.error("replication from error",err)}};let filter_tables=[];for(let doc of rep_docs){if(!filter_tables.includes(doc.table_id))filter_tables.push(doc.table_id);if(doc.stat<3)await download_documents(doc)}try{_this.db_changes_store=remote_db.changes({since:"now",live:true,timeout:3e5,style:"main_only",include_docs:false}).on("change",async function(change){if(change.id.substr(0,4)!=="dbs-")return;if(_this.db_replication_from_stat!==3){set_replication_stat(SESSION_ID,3)}try{const existing_doc=await db.get(change.id);db.replicate.from(remote_db_url,{doc_ids:[change.id],retry:true,style:"main_only",fetch:function(url,opts){if(_session.gtp_token){opts.headers.set("xu-gtp-token",_session.gtp_token)}if(_session.app_token)opts.headers.set("xu-app-token",_session.app_token);{opts.headers.set("xu-db",db_name)}return PouchDB.fetch(url,opts)}})}catch(err){try{const table_id=change.id.split("_")[0].substr(4);for(let doc of rep_docs){if(doc.table_id===table_id){download_documents(doc)}}}catch(err){console.warn(err)}}}).on("complete",function(info){}).on("error",function(err){console.error(err);set_replication_stat(SESSION_ID,2);_this.db_changes_store.cancel()})}catch(err){console.error(err)}return};_db.pouch.remove_db_replication_from_server=async function(SESSION_ID,rep_id){const db=await func.utils.connect_pouchdb(SESSION_ID);try{const doc=await db.get(rep_id);db.remove(doc)}catch(error){}};_db.pouch.get_replications=async function(SESSION_ID=Object.keys(SESSION_OBJ)[0]){const db=await func.utils.connect_pouchdb(SESSION_ID);let ret=await db.find({selector:{docType:"rep_request"}});return ret.docs};_db.pouch.get_replication_stat=async function(SESSION_ID){const _session=_this.SESSION_OBJ[SESSION_ID];return _session?.DS_GLB?.[0]?.data_system?.SYS_GLOBAL_BOL_REPLICATION_STAT};const set_replication_stat=async function(SESSION_ID,from_stat,to_stat){var _session=_this.SESSION_OBJ[SESSION_ID];if(!_session?.DS_GLB?.[0]){return setTimeout(function(){set_replication_stat(SESSION_ID,from_stat,to_stat)},1e3)}if(from_stat)_this.db_replication_from_stat=from_stat;if(to_stat)_this.db_replication_to_stat=to_stat;var data={};if(_this.db_replication_from_stat===3&&_this.db_replication_to_stat===3){data.SYS_GLOBAL_BOL_REPLICATION_STAT=1;console.info("Replication On")}else{data.SYS_GLOBAL_BOL_REPLICATION_STAT=0;console.info("Replication Off")}var datasource_changes={[0]:{["data_system"]:data}};await func.datasource.update(SESSION_ID,datasource_changes)};const refresh_document_changes_for_realtime_update=function(SESSION_ID,doc_change){var _session=_this.SESSION_OBJ[SESSION_ID];if(glb.IS_WORKER){return func.utils.post_back_to_client(SESSION_ID,"refresh_document_changes_for_realtime_update",_session.worker_id,{doc_change:doc_change})}func.UI.screen.refresh_document_changes_for_realtime_update(SESSION_ID,doc_change)};
|
|
@@ -657,6 +657,8 @@ _db.pouch.init_db_replication = async function (SESSION_ID) {
|
|
|
657
657
|
_db.pouch.set_db_replication_from_server(SESSION_ID);
|
|
658
658
|
};
|
|
659
659
|
_db.pouch.set_db_replication_from_server = async function (SESSION_ID) {
|
|
660
|
+
if (!navigator.onLine) return;
|
|
661
|
+
|
|
660
662
|
const db = await func.utils.connect_pouchdb(SESSION_ID);
|
|
661
663
|
const rep_docs = await _db.pouch.get_replications(SESSION_ID);
|
|
662
664
|
const _session = _this.SESSION_OBJ[SESSION_ID];
|
|
@@ -37,19 +37,15 @@ export const project_loader = async function (SESSION_ID, app_id, prog_id) {
|
|
|
37
37
|
|
|
38
38
|
try {
|
|
39
39
|
const { _id, _rev } = await db.get(`cache_build_info`);
|
|
40
|
-
await db.
|
|
41
|
-
} catch (error) {
|
|
42
|
-
debugger;
|
|
43
|
-
}
|
|
40
|
+
await db.remove({ _id, _rev });
|
|
41
|
+
} catch (error) {}
|
|
44
42
|
try {
|
|
45
43
|
await db.put({
|
|
46
44
|
_id: `cache_build_info`,
|
|
47
45
|
build_info: ret_build_info,
|
|
48
46
|
docType: 'cache_build_info',
|
|
49
47
|
});
|
|
50
|
-
} catch (error) {
|
|
51
|
-
debugger;
|
|
52
|
-
}
|
|
48
|
+
} catch (error) {}
|
|
53
49
|
} else {
|
|
54
50
|
// off-line get from pouch
|
|
55
51
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const _this={};export const init_module=e=>{_this.func=e.func;_this.glb=e.glb;_this.SESSION_OBJ=e.SESSION_OBJ;_this.APP_OBJ=e.APP_OBJ;_this.IS_DOCKER=e.IS_DOCKER;_this.IS_API_SERVER=e.IS_API_SERVER;_this.IS_PROCESS_SERVER=e.IS_PROCESS_SERVER};export const project_loader=async function(SESSION_ID,app_id,prog_id){try{var _session=_this.SESSION_OBJ[SESSION_ID];if(_this.func.UI.utils.get_url_attribute(SESSION_ID,"clear_cache")){await func.index.delete_pouch(SESSION_ID)}let last_changed_ts=0;var ret_build_info={};if(typeof XUDA_BUILD_INFO!=="undefined"){_session.build_info=XUDA_BUILD_INFO}else{const db=await func.utils.connect_pouchdb(SESSION_ID);if(["live_preview","miniapp"].includes(_session.engine_mode)){if(navigator.onLine){ret_build_info=await get_app_build_info(SESSION_ID,app_id);if(ret_build_info.code<0){return console.error(ret_build_info)}try{const{_id,_rev}=await db.get(`cache_build_info`);await db.destroy({_id:_id,_rev:_rev})}catch(error){debugger}try{await db.put({_id:`cache_build_info`,build_info:ret_build_info,docType:"cache_build_info"})}catch(error){debugger}}else{try{ret_build_info=(await db.get(`cache_build_info`)).build_info}catch(error){return console.error("cache_build_info error")}}last_changed_ts=ret_build_info.data.last_changed_ts}else{try{await db.get(`cache_rt_info`)}catch(err){const startup_module=await func.common.get_module(SESSION_ID,"xuda-deploy-startup-loader.mjs");await startup_module.loader(SESSION_ID)}last_changed_ts=_session.opt.last_changed_ts}}await get_rt_info(SESSION_ID,app_id,last_changed_ts);insert_custom_prop(SESSION_ID);if(_session?.app_admin_prop?.app_admin_direction){$(_session.root_element).attr("dir",_session.app_admin_prop.app_admin_direction)}if(_session.engine_mode==="live_preview"){const module=await _this.func.common.get_module(SESSION_ID,"xuda-live-preview-module.esm.js");module.live_preview_loader(SESSION_ID);return}if(_this.APP_OBJ[app_id]?.is_deployment){try{await init_runtime_websocket(SESSION_ID,app_id)}catch(error){throw error}}const module=await _this.func.common.get_module(SESSION_ID,`xuda-progs-loader-module.mjs`);if(app_id!=="unknown"){await module.load_objects_cache(SESSION_ID)}await _this.func.UI.main.embed_loader(SESSION_ID)}catch(error){throw error}};const get_app_build_info=async function(SESSION_ID,app_id){return new Promise(function(resolve,reject){var _session=_this.SESSION_OBJ[SESSION_ID];let app_id_reference=_this.APP_OBJ[app_id].app_id_reference;fetch(_this.func.common.get_url(SESSION_ID,"rpi","get_app_build"),{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","xu-gtp-token":_session.gtp_token,"xu-app-token":_session.app_token},body:JSON.stringify({app_id:app_id,app_id_reference:app_id_reference,engine_mode:_session.engine_mode})}).then(response=>{if(!response.ok){return response.text().then(text=>{throw new Error(text)})}return response.json()}).then(async json=>{_this.SESSION_OBJ[SESSION_ID].build_info=json.data;resolve(json)}).catch(err=>{try{resolve(JSON.parse(err.message))}catch(error){resolve({code:-1,data:err.message})}})})};const get_user_group_account_info=async function(SESSION_ID,uid){var _session=SESSION_OBJ[SESSION_ID];const response=await fetch(`https://${_session.domain}/cpi/get_account_info`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify({uid:uid,uid_query:$.cookie("uid")})});const json=await response.json();return json.data};const get_rt_info=async function(SESSION_ID,app_id,last_changed_ts){var _session=_this.SESSION_OBJ[SESSION_ID];return new Promise(async function(resolve,reject){const response={success:async function(ret,ajaxP){if(ret.code<0){return response.error(ret.data)}const rt_info_obj=ret.data;var app_id=rt_info_obj._id;_this.APP_OBJ[app_id]=rt_info_obj;_session.app_id=app_id;if(rt_info_obj?.deploy_data?.global_variables){_session.url_params={..._session.url_params,...rt_info_obj.deploy_data.global_variables}}let account_info=_.clone(rt_info_obj?.account_info||{});if(_session.engine_mode==="user_group"){const user_group_data=await get_user_group_account_info(SESSION_ID,rt_info_obj?.account_info?.uid);account_info=user_group_data.account_info;account_info.uid=user_group_data._id}_session.USR_OBJ={_id:account_info?.uid,usr_name:account_info?.username,usr_first_name:account_info?.first_name||account_info?.email,usr_last_name:account_info?.last_name||"",usr_email:account_info?.email,usr_profile_picture:account_info?.profile_picture};_session.login_info=rt_info_obj?.login_info;_session.client_ip=rt_info_obj.client_ip;_session.rpi_http_methods=rt_info_obj.rpi_http_methods;_session.app_admin_prop=rt_info_obj.app_admin_prop;_session.is_deployment=rt_info_obj.is_deployment;resolve();$(".loader_logo").css("background-image",`url( ${rt_info_obj.app_pic})`).hide().fadeIn()},error:async function(err){if(err){return _this.func.UI.utils.progressScreen.show(SESSION_ID,err,null,true)}console.warn("** reload request")}};const db=await func.utils.connect_pouchdb(SESSION_ID);try{let ret=await db.get(`cache_rt_info`);const rt_info_obj=ret.data;if(rt_info_obj.data.last_changed_ts!==last_changed_ts||!["live_preview","miniapp"].includes(_session.engine_mode)&&rt_info_obj.data.app_build_id!==_session.opt.app_build_id){_this.func.UI.utils.progressScreen.show(SESSION_ID,"New application setup detected, refreshing data and reloading in 5 sec");setTimeout(async()=>{await func.index.delete_pouch(SESSION_ID);location.reload()},5e3);return}response.success(rt_info_obj)}catch(err){fetch(_this.func.common.get_url(SESSION_ID,"rpi","get_rt_info"),{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","xu-gtp-token":_session.gtp_token,"xu-app-token":_session.app_token},body:JSON.stringify({app_id:app_id,prog_id:_session.engine_mode==="live_preview"?"":_session.prog_id,engine_mode:_session.engine_mode,build:_session.build_info.build,session_id:SESSION_ID,is_cordova:_this.glb.is_cordova,client_info:_session.SYS_GLOBAL_OBJ_CLIENT_INFO,client_id:_session.SYS_GLOBAL_OBJ_CLIENT_INFO.fingerprint})}).then(response=>{if(!response.ok){return response.text().then(text=>{throw new Error(text)})}return response.json()}).then(json=>{if(json.code<0){throw new Error(json.data||"unknown error")}response.success(json);var doc={_id:`cache_rt_info`,data:json,docType:"cache_app"};db.put(doc)}).catch(err=>{response.error(err.message)})}})};const insert_custom_prop=function(SESSION_ID){try{var app_id=_this.SESSION_OBJ[SESSION_ID].app_id;if(_this.APP_OBJ[app_id]?.app_custom_prop?.app_custom_header){var head=document.getElementsByTagName("head")[0];const app_custom_header=_this.APP_OBJ[app_id].app_custom_prop.app_custom_header;$(head).append(func.utils.replace_studio_drive_url(SESSION_ID,app_custom_header))}if(_this.APP_OBJ[app_id]?.app_custom_prop?.app_custom_body){$(_this.SESSION_OBJ[SESSION_ID].root_element).prepend(_this.APP_OBJ[app_id].app_custom_prop.app_custom_body)}}catch(err){console.error(err)}};const init_runtime_websocket=function(SESSION_ID,app_id){return new Promise(function(resolve,reject){const set_connected=async function(stat){var datasource_changes={[0]:{["data_system"]:{SYS_GLOBAL_BOL_CONNECTED:stat}}};await func.datasource.update(SESSION_ID,datasource_changes)};var _session=_this.SESSION_OBJ[SESSION_ID];var _data_system=_session?.DS_GLB?.[0]?.data_system;const url=`https://${location.hostname}`;var error;RUNTIME_SERVER_WEBSOCKET=io(url,{secure:true,reconnection:true,rejectUnauthorized:false,path:"/ws/socket.io"});RUNTIME_SERVER_WEBSOCKET.on("connect",()=>{console.info("RUNTIME_SERVER_WEBSOCKET connected");if(_data_system){set_connected(1)}if(_session.opt.enable_offline){if($(_session.root_element).hasClass("runtime_offline")){$(_session.root_element).removeClass("runtime_offline");func.utils.alerts.toast(SESSION_ID,"Switched to on-line mode","You are now online. All data stored while you were offline will be synchronized to the server.","success")}}else{_this.func.UI.utils.progressScreen.hide(SESSION_ID)}if(error){if(!RUNTIME_SERVER_WEBSOCKET_CONNECTED){console.warn("** reload request")}}});RUNTIME_SERVER_WEBSOCKET.on("message",e=>{if(_this.APP_OBJ[app_id].is_deployment){_this.func.UI.utils.indicator.server.busy();setTimeout(function(){_this.func.UI.utils.indicator.server.normal()},e?.data?.length*100||100)}var data=e.data;if(e.source==="http_call"){if(e.service==="get_doc_obj_from_build"){return $("body").trigger("get_doc_obj_from_build_response_"+data._id,{data:data})}if(e.service==="heartbeat"){return $("body").trigger("heartbeat_response",{data:data})}return $("body").trigger("get_ws_data_response_"+e.websocket_queue_num,{data:data,e:e})}if(e.source==="deployment_server"){console.log("document_changed",e);return func.UI.screen.refresh_document_changes_for_realtime_update(SESSION_ID,e.data)}if(data!=="connected")return;RUNTIME_SERVER_WEBSOCKET_CONNECTED=true;resolve()});var callback_done=false;RUNTIME_SERVER_WEBSOCKET.on("connect_error",error=>{if(!callback_done){resolve();callback_done=true;error=true}});RUNTIME_SERVER_WEBSOCKET.on("disconnect",async()=>{RUNTIME_SERVER_WEBSOCKET_CONNECTED=false;if(_data_system){set_connected(0)}if(_session.opt.enable_offline){$(_session.root_element).addClass("runtime_offline");func.utils.alerts.toast(SESSION_ID,"Switched to off-line mode","You have lost connection to the server and are now working offline. Once the connection is restored, all data will be synchronized.","warning")}else{_this.func.UI.utils.progressScreen.show(SESSION_ID,"Your browser has temporarily disconnected from the server. Please wait while we attempt to reconnect.")}});window.addEventListener("beforeunload",function(event){var obj={service:"close_websocket",data:{session_id:SESSION_ID}};RUNTIME_SERVER_WEBSOCKET.emit("message",obj)})})};export const run_plugins_runtime_init=async function(SESSION_ID,app_id,method){var _session=_this.SESSION_OBJ[SESSION_ID];const get_path=function(plugin_name,resource){if(_session.worker_type==="Dev"){return`../../plugins/${_session.domain}/${plugin_name}/${resource}`}return`https://${_session.domain}/plugins/${plugin_name}/runtime/${resource}?gtp_token=${_session.gtp_token}&app_id=${_session.app_id}`};if(!_this.APP_OBJ[app_id].app_plugins_purchased)return;for await(const[key,val]of Object.entries(_this.APP_OBJ[app_id].app_plugins_purchased)){if(!val.installed)continue;if(val.manifest?.[method].mjs?.exist&&!val.manifest[method].mjs.is_empty){const module=await import(get_path(key,method+".mjs"));try{await module.default({SESSION_ID:SESSION_ID})}catch(error){console.error(error)}}}};
|
|
1
|
+
const _this={};export const init_module=e=>{_this.func=e.func;_this.glb=e.glb;_this.SESSION_OBJ=e.SESSION_OBJ;_this.APP_OBJ=e.APP_OBJ;_this.IS_DOCKER=e.IS_DOCKER;_this.IS_API_SERVER=e.IS_API_SERVER;_this.IS_PROCESS_SERVER=e.IS_PROCESS_SERVER};export const project_loader=async function(SESSION_ID,app_id,prog_id){try{var _session=_this.SESSION_OBJ[SESSION_ID];if(_this.func.UI.utils.get_url_attribute(SESSION_ID,"clear_cache")){await func.index.delete_pouch(SESSION_ID)}let last_changed_ts=0;var ret_build_info={};if(typeof XUDA_BUILD_INFO!=="undefined"){_session.build_info=XUDA_BUILD_INFO}else{const db=await func.utils.connect_pouchdb(SESSION_ID);if(["live_preview","miniapp"].includes(_session.engine_mode)){if(navigator.onLine){ret_build_info=await get_app_build_info(SESSION_ID,app_id);if(ret_build_info.code<0){return console.error(ret_build_info)}try{const{_id,_rev}=await db.get(`cache_build_info`);await db.remove({_id:_id,_rev:_rev})}catch(error){}try{await db.put({_id:`cache_build_info`,build_info:ret_build_info,docType:"cache_build_info"})}catch(error){}}else{try{ret_build_info=(await db.get(`cache_build_info`)).build_info}catch(error){return console.error("cache_build_info error")}}last_changed_ts=ret_build_info.data.last_changed_ts}else{try{await db.get(`cache_rt_info`)}catch(err){const startup_module=await func.common.get_module(SESSION_ID,"xuda-deploy-startup-loader.mjs");await startup_module.loader(SESSION_ID)}last_changed_ts=_session.opt.last_changed_ts}}await get_rt_info(SESSION_ID,app_id,last_changed_ts);insert_custom_prop(SESSION_ID);if(_session?.app_admin_prop?.app_admin_direction){$(_session.root_element).attr("dir",_session.app_admin_prop.app_admin_direction)}if(_session.engine_mode==="live_preview"){const module=await _this.func.common.get_module(SESSION_ID,"xuda-live-preview-module.esm.js");module.live_preview_loader(SESSION_ID);return}if(_this.APP_OBJ[app_id]?.is_deployment){try{await init_runtime_websocket(SESSION_ID,app_id)}catch(error){throw error}}const module=await _this.func.common.get_module(SESSION_ID,`xuda-progs-loader-module.mjs`);if(app_id!=="unknown"){await module.load_objects_cache(SESSION_ID)}await _this.func.UI.main.embed_loader(SESSION_ID)}catch(error){throw error}};const get_app_build_info=async function(SESSION_ID,app_id){return new Promise(function(resolve,reject){var _session=_this.SESSION_OBJ[SESSION_ID];let app_id_reference=_this.APP_OBJ[app_id].app_id_reference;fetch(_this.func.common.get_url(SESSION_ID,"rpi","get_app_build"),{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","xu-gtp-token":_session.gtp_token,"xu-app-token":_session.app_token},body:JSON.stringify({app_id:app_id,app_id_reference:app_id_reference,engine_mode:_session.engine_mode})}).then(response=>{if(!response.ok){return response.text().then(text=>{throw new Error(text)})}return response.json()}).then(async json=>{_this.SESSION_OBJ[SESSION_ID].build_info=json.data;resolve(json)}).catch(err=>{try{resolve(JSON.parse(err.message))}catch(error){resolve({code:-1,data:err.message})}})})};const get_user_group_account_info=async function(SESSION_ID,uid){var _session=SESSION_OBJ[SESSION_ID];const response=await fetch(`https://${_session.domain}/cpi/get_account_info`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify({uid:uid,uid_query:$.cookie("uid")})});const json=await response.json();return json.data};const get_rt_info=async function(SESSION_ID,app_id,last_changed_ts){var _session=_this.SESSION_OBJ[SESSION_ID];return new Promise(async function(resolve,reject){const response={success:async function(ret,ajaxP){if(ret.code<0){return response.error(ret.data)}const rt_info_obj=ret.data;var app_id=rt_info_obj._id;_this.APP_OBJ[app_id]=rt_info_obj;_session.app_id=app_id;if(rt_info_obj?.deploy_data?.global_variables){_session.url_params={..._session.url_params,...rt_info_obj.deploy_data.global_variables}}let account_info=_.clone(rt_info_obj?.account_info||{});if(_session.engine_mode==="user_group"){const user_group_data=await get_user_group_account_info(SESSION_ID,rt_info_obj?.account_info?.uid);account_info=user_group_data.account_info;account_info.uid=user_group_data._id}_session.USR_OBJ={_id:account_info?.uid,usr_name:account_info?.username,usr_first_name:account_info?.first_name||account_info?.email,usr_last_name:account_info?.last_name||"",usr_email:account_info?.email,usr_profile_picture:account_info?.profile_picture};_session.login_info=rt_info_obj?.login_info;_session.client_ip=rt_info_obj.client_ip;_session.rpi_http_methods=rt_info_obj.rpi_http_methods;_session.app_admin_prop=rt_info_obj.app_admin_prop;_session.is_deployment=rt_info_obj.is_deployment;resolve();$(".loader_logo").css("background-image",`url( ${rt_info_obj.app_pic})`).hide().fadeIn()},error:async function(err){if(err){return _this.func.UI.utils.progressScreen.show(SESSION_ID,err,null,true)}console.warn("** reload request")}};const db=await func.utils.connect_pouchdb(SESSION_ID);try{let ret=await db.get(`cache_rt_info`);const rt_info_obj=ret.data;if(rt_info_obj.data.last_changed_ts!==last_changed_ts||!["live_preview","miniapp"].includes(_session.engine_mode)&&rt_info_obj.data.app_build_id!==_session.opt.app_build_id){_this.func.UI.utils.progressScreen.show(SESSION_ID,"New application setup detected, refreshing data and reloading in 5 sec");setTimeout(async()=>{await func.index.delete_pouch(SESSION_ID);location.reload()},5e3);return}response.success(rt_info_obj)}catch(err){fetch(_this.func.common.get_url(SESSION_ID,"rpi","get_rt_info"),{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","xu-gtp-token":_session.gtp_token,"xu-app-token":_session.app_token},body:JSON.stringify({app_id:app_id,prog_id:_session.engine_mode==="live_preview"?"":_session.prog_id,engine_mode:_session.engine_mode,build:_session.build_info.build,session_id:SESSION_ID,is_cordova:_this.glb.is_cordova,client_info:_session.SYS_GLOBAL_OBJ_CLIENT_INFO,client_id:_session.SYS_GLOBAL_OBJ_CLIENT_INFO.fingerprint})}).then(response=>{if(!response.ok){return response.text().then(text=>{throw new Error(text)})}return response.json()}).then(json=>{if(json.code<0){throw new Error(json.data||"unknown error")}response.success(json);var doc={_id:`cache_rt_info`,data:json,docType:"cache_app"};db.put(doc)}).catch(err=>{response.error(err.message)})}})};const insert_custom_prop=function(SESSION_ID){try{var app_id=_this.SESSION_OBJ[SESSION_ID].app_id;if(_this.APP_OBJ[app_id]?.app_custom_prop?.app_custom_header){var head=document.getElementsByTagName("head")[0];const app_custom_header=_this.APP_OBJ[app_id].app_custom_prop.app_custom_header;$(head).append(func.utils.replace_studio_drive_url(SESSION_ID,app_custom_header))}if(_this.APP_OBJ[app_id]?.app_custom_prop?.app_custom_body){$(_this.SESSION_OBJ[SESSION_ID].root_element).prepend(_this.APP_OBJ[app_id].app_custom_prop.app_custom_body)}}catch(err){console.error(err)}};const init_runtime_websocket=function(SESSION_ID,app_id){return new Promise(function(resolve,reject){const set_connected=async function(stat){var datasource_changes={[0]:{["data_system"]:{SYS_GLOBAL_BOL_CONNECTED:stat}}};await func.datasource.update(SESSION_ID,datasource_changes)};var _session=_this.SESSION_OBJ[SESSION_ID];var _data_system=_session?.DS_GLB?.[0]?.data_system;const url=`https://${location.hostname}`;var error;RUNTIME_SERVER_WEBSOCKET=io(url,{secure:true,reconnection:true,rejectUnauthorized:false,path:"/ws/socket.io"});RUNTIME_SERVER_WEBSOCKET.on("connect",()=>{console.info("RUNTIME_SERVER_WEBSOCKET connected");if(_data_system){set_connected(1)}if(_session.opt.enable_offline){if($(_session.root_element).hasClass("runtime_offline")){$(_session.root_element).removeClass("runtime_offline");func.utils.alerts.toast(SESSION_ID,"Switched to on-line mode","You are now online. All data stored while you were offline will be synchronized to the server.","success")}}else{_this.func.UI.utils.progressScreen.hide(SESSION_ID)}if(error){if(!RUNTIME_SERVER_WEBSOCKET_CONNECTED){console.warn("** reload request")}}});RUNTIME_SERVER_WEBSOCKET.on("message",e=>{if(_this.APP_OBJ[app_id].is_deployment){_this.func.UI.utils.indicator.server.busy();setTimeout(function(){_this.func.UI.utils.indicator.server.normal()},e?.data?.length*100||100)}var data=e.data;if(e.source==="http_call"){if(e.service==="get_doc_obj_from_build"){return $("body").trigger("get_doc_obj_from_build_response_"+data._id,{data:data})}if(e.service==="heartbeat"){return $("body").trigger("heartbeat_response",{data:data})}return $("body").trigger("get_ws_data_response_"+e.websocket_queue_num,{data:data,e:e})}if(e.source==="deployment_server"){console.log("document_changed",e);return func.UI.screen.refresh_document_changes_for_realtime_update(SESSION_ID,e.data)}if(data!=="connected")return;RUNTIME_SERVER_WEBSOCKET_CONNECTED=true;resolve()});var callback_done=false;RUNTIME_SERVER_WEBSOCKET.on("connect_error",error=>{if(!callback_done){resolve();callback_done=true;error=true}});RUNTIME_SERVER_WEBSOCKET.on("disconnect",async()=>{RUNTIME_SERVER_WEBSOCKET_CONNECTED=false;if(_data_system){set_connected(0)}if(_session.opt.enable_offline){$(_session.root_element).addClass("runtime_offline");func.utils.alerts.toast(SESSION_ID,"Switched to off-line mode","You have lost connection to the server and are now working offline. Once the connection is restored, all data will be synchronized.","warning")}else{_this.func.UI.utils.progressScreen.show(SESSION_ID,"Your browser has temporarily disconnected from the server. Please wait while we attempt to reconnect.")}});window.addEventListener("beforeunload",function(event){var obj={service:"close_websocket",data:{session_id:SESSION_ID}};RUNTIME_SERVER_WEBSOCKET.emit("message",obj)})})};export const run_plugins_runtime_init=async function(SESSION_ID,app_id,method){var _session=_this.SESSION_OBJ[SESSION_ID];const get_path=function(plugin_name,resource){if(_session.worker_type==="Dev"){return`../../plugins/${_session.domain}/${plugin_name}/${resource}`}return`https://${_session.domain}/plugins/${plugin_name}/runtime/${resource}?gtp_token=${_session.gtp_token}&app_id=${_session.app_id}`};if(!_this.APP_OBJ[app_id].app_plugins_purchased)return;for await(const[key,val]of Object.entries(_this.APP_OBJ[app_id].app_plugins_purchased)){if(!val.installed)continue;if(val.manifest?.[method].mjs?.exist&&!val.manifest[method].mjs.is_empty){const module=await import(get_path(key,method+".mjs"));try{await module.default({SESSION_ID:SESSION_ID})}catch(error){console.error(error)}}}};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xuda.io/runtime-bundle",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1224",
|
|
4
4
|
"description": "The Xuda Runtime Bundle refers to a collection of scripts and libraries packaged together to provide the necessary runtime environment for executing plugins or components in the Xuda platform. ",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"pub": "npm version patch --force && npm publish --access public"
|