@xuda.io/xuda-progs-loader-module 1.0.0

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.
Files changed (2) hide show
  1. package/index.mjs +305 -0
  2. package/package.json +8 -0
package/index.mjs ADDED
@@ -0,0 +1,305 @@
1
+ const _this = {};
2
+
3
+ export const init_module = (e) => {
4
+ _this.func = e.func;
5
+ _this.glb = e.glb;
6
+ _this.SESSION_OBJ = e.SESSION_OBJ;
7
+ _this.APP_OBJ = e.APP_OBJ;
8
+ _this.IS_DOCKER = e.IS_DOCKER;
9
+ _this.IS_API_SERVER = e.IS_API_SERVER;
10
+ _this.IS_PROCESS_SERVER = e.IS_PROCESS_SERVER;
11
+ };
12
+
13
+ export const get_doc_from_studio = async function (SESSION_ID, app_id, idP) {
14
+ return new Promise(function (resolve, reject) {
15
+ var _session = _this.SESSION_OBJ[SESSION_ID];
16
+ if (glb.IS_WORKER) {
17
+ func.utils.post_back_to_client(SESSION_ID, 'get_doc_from_studio', _session.worker_id, { doc_id: idP });
18
+
19
+ self.addEventListener('live_preview_get_obj_response_worker_' + idP, (event) => {
20
+ // console.log("Custom event triggered:", event);
21
+ resolve(event.detail.data);
22
+ });
23
+ } else {
24
+ STUDIO_PEER_CONN_SEND_METHOD({
25
+ service: 'get_doc_obj',
26
+ data: { doc_id: idP },
27
+ session_id: SESSION_ID,
28
+ id: STUDIO_PEER.id,
29
+ source: 'runtime',
30
+ app_id,
31
+ });
32
+
33
+ $('body').on('live_preview_get_obj_response_' + idP, function (e, data) {
34
+ resolve(data.data);
35
+ $('body').off('live_preview_get_obj_response_' + idP);
36
+ });
37
+ }
38
+ });
39
+ };
40
+
41
+ export const get_doc_from_websocket = async function (SESSION_ID, app_id, idP) {
42
+ var _session = _this.SESSION_OBJ[SESSION_ID];
43
+ return new Promise(function (resolve, reject) {
44
+ if (glb.IS_WORKER) {
45
+ // throw new Error("fail to fetch from ws");
46
+ func.utils.post_back_to_client(SESSION_ID, 'get_doc_from_websocket', _session.worker_id, { doc_id: idP });
47
+
48
+ self.addEventListener('get_doc_obj_from_build_worker_' + idP, (event) => {
49
+ resolve(event.detail.data);
50
+ });
51
+ } else {
52
+ if (RUNTIME_SERVER_WEBSOCKET && RUNTIME_SERVER_WEBSOCKET_CONNECTED) {
53
+ RUNTIME_SERVER_WEBSOCKET.emit('message', {
54
+ service: 'get_doc_obj_from_build',
55
+ data: { doc_id: idP },
56
+ });
57
+ $('body').on('get_doc_obj_from_build_response_' + idP, (e, data) => {
58
+ // views_ret = data.data;
59
+ resolve(data.data);
60
+ $('body').off('get_doc_obj_from_build_response_' + idP);
61
+ });
62
+ } else {
63
+ throw new Error('fail to fetch from ws websocket inactive');
64
+ }
65
+ }
66
+ });
67
+ };
68
+
69
+ export const DOCS_OBJ_get = async function (SESSION_ID, idP) {
70
+ if (!idP) return;
71
+
72
+ if (idP === '_empty_panel_program') {
73
+ return {
74
+ _id: '_empty_panel_program',
75
+
76
+ properties: {
77
+ menuName: '_empty_panel_program',
78
+ menuTitle: '_empty_panel_program',
79
+ menuType: 'component',
80
+ // uiFramework: "@xuda.io/xuda-framework-plugin-tailwind",
81
+ renderType: 'form',
82
+ },
83
+ progUi: [
84
+ {
85
+ id: 'node-empty_panel_program',
86
+ tagName: 'xu-single-view',
87
+ children: [
88
+ {
89
+ id: 'node-empty_panel_program-span',
90
+ tagName: 'span',
91
+ children: [],
92
+ content: '',
93
+ type: 'element',
94
+ attributes: {},
95
+ },
96
+ ],
97
+ content: '',
98
+ type: 'element',
99
+ attributes: {},
100
+ },
101
+ ],
102
+ progDataSource: {},
103
+ progFields: [],
104
+ progEvents: [],
105
+ };
106
+ }
107
+
108
+ var _session = _this.SESSION_OBJ[SESSION_ID];
109
+ const app_id = _session.app_id;
110
+ var views_ret;
111
+
112
+ if (_session.engine_mode === 'live_preview') {
113
+ return await get_doc_from_studio(SESSION_ID, app_id, idP);
114
+ }
115
+
116
+ if (glb.register_startup_programs) {
117
+ func.common.db(SESSION_ID, 'register_startup_programs', {
118
+ prog_id: idP,
119
+ build_id: _session?.build_info?.build || 0,
120
+ });
121
+ }
122
+
123
+ views_ret = await read_objects_cache(SESSION_ID, idP, 'DOCS_OBJ');
124
+
125
+ if (!views_ret) {
126
+ if (typeof _this.IS_DOCKER === 'undefined' && typeof _this.IS_PROCESS_SERVER === 'undefined') {
127
+ let url = _this.func.common.get_url(SESSION_ID, 'rpi', '');
128
+
129
+ // Jul 7,25 build condition for master need to be added
130
+ if (['master'].includes(_this.APP_OBJ[app_id].app_type)) {
131
+ url += 'get_doc_obj_raw';
132
+ } else {
133
+ url += 'get_doc_obj_from_build';
134
+ }
135
+
136
+ let app_id_reference = _this.APP_OBJ[app_id].app_id_reference;
137
+
138
+ /////////////////
139
+
140
+ const http = async function () {
141
+ const response = await fetch(url, {
142
+ method: 'POST',
143
+ headers: {
144
+ Accept: 'application/json',
145
+ 'Content-Type': 'application/json',
146
+ 'xu-gtp-token': _session.gtp_token,
147
+ 'xu-app-token': _session.app_token,
148
+ },
149
+ body: JSON.stringify({
150
+ app_id: app_id,
151
+ doc_id: idP,
152
+ build: _session.build_info.instance_build || _session.build_info.build || _session.opt.app_build_id,
153
+ app_id_reference: app_id_reference,
154
+ engine_mode: _session.engine_mode,
155
+ }),
156
+ });
157
+ const json = await response.json();
158
+ if (response) {
159
+ if (response && response.status === 400) {
160
+ return _this.func.utils.request_error(SESSION_ID, 'ajax', response.statusText);
161
+ }
162
+
163
+ views_ret = json.data;
164
+ }
165
+ };
166
+
167
+ if (_session.engine_mode === 'miniapp') {
168
+ await http();
169
+ } else {
170
+ views_ret = await get_doc_from_websocket(SESSION_ID, app_id, idP);
171
+ }
172
+
173
+ // // Jul 7,25 replaced ^^
174
+ // try {
175
+ // if (SESSION_OBJ?.[SESSION_ID]?.rpi_http_methods?.includes('get_doc_obj_from_build')) {
176
+ // views_ret = await get_doc_from_websocket(SESSION_ID, app_id, idP);
177
+ // } else {
178
+ // throw new Error('not a get_doc_obj_from_build method');
179
+ // }
180
+ // } catch (err) {
181
+ // await http();
182
+ // }
183
+ }
184
+
185
+ if (typeof _this.IS_DOCKER !== 'undefined' || typeof _this.IS_PROCESS_SERVER !== 'undefined') {
186
+ var ret = await __.rpi.get_prog(app_id, idP);
187
+
188
+ if (ret.data) {
189
+ views_ret = ret.data;
190
+ }
191
+ }
192
+
193
+ if (views_ret) {
194
+ if (views_ret.error) {
195
+ console.error(views_ret);
196
+ views_ret = '';
197
+ return;
198
+ }
199
+ await save_objects_cache(SESSION_ID, idP, 'DOCS_OBJ', views_ret);
200
+ }
201
+ }
202
+
203
+ return views_ret;
204
+ };
205
+
206
+ export const save_objects_cache = async function (SESSION_ID, id, obj_type, data) {
207
+ var _session = _this.SESSION_OBJ[SESSION_ID];
208
+ if (typeof _this.IS_DOCKER !== 'undefined' || typeof _this.IS_PROCESS_SERVER !== 'undefined' || _session.worker_type === 'Dev') {
209
+ return;
210
+ }
211
+
212
+ var build_id = _session?.build_info?.build || 0;
213
+
214
+ const db = await _this.func.utils.connect_pouchdb(SESSION_ID);
215
+ var doc = {
216
+ _id: `cache_${build_id}_${obj_type}_${id}`,
217
+ id: id,
218
+ docType: 'cache_objects',
219
+ build_id: build_id.toString(),
220
+ obj_type: obj_type,
221
+ data: data,
222
+ };
223
+ setTimeout(async function () {
224
+ try {
225
+ await db.get(doc_id);
226
+ return;
227
+ } catch (e) {
228
+ if (await read_objects_cache(SESSION_ID, id, obj_type)) {
229
+ return;
230
+ }
231
+ if (!doc.data || (doc.data && typeof doc.data === 'string' && doc.data.indexOf('error') > -1)) {
232
+ return;
233
+ }
234
+
235
+ try {
236
+ return await db.put(doc);
237
+ } catch (err) {
238
+ return;
239
+ }
240
+ }
241
+ }, 1000);
242
+ };
243
+ export const read_objects_cache = async function (SESSION_ID, id, obj_type) {
244
+ if (typeof _this.IS_DOCKER !== 'undefined' || typeof _this.IS_PROCESS_SERVER !== 'undefined') {
245
+ try {
246
+ const data = fs.readFileSync('/var/xuda/progs/' + id + '.json', 'utf8');
247
+ return JSON.parse(data);
248
+ } catch (err) {
249
+ console.error('read_objects_cache', err);
250
+ return;
251
+ }
252
+ }
253
+ var _session = _this.SESSION_OBJ[SESSION_ID];
254
+
255
+ var build_id = _session?.build_info?.build || 0;
256
+
257
+ const db = await _this.func.utils.connect_pouchdb(SESSION_ID);
258
+ var ret;
259
+ try {
260
+ ret = await db.get(`cache_${build_id}_${obj_type}_${id}`);
261
+
262
+ if (ret.data && typeof ret.data === 'string' && ret.data.indexOf('error') > -1) {
263
+ return '';
264
+ }
265
+
266
+ return ret.data;
267
+ } catch (err) {
268
+ return ret;
269
+ }
270
+ };
271
+ export const delete_objects_cache = async function (SESSION_ID, id, obj_type) {
272
+ const app_id = _this.SESSION_OBJ[SESSION_ID].app_id;
273
+
274
+ var build_id = _session?.build_info?.build || 0;
275
+ const db = await _this.func.utils.connect_pouchdb(SESSION_ID);
276
+
277
+ try {
278
+ var doc = await db.get(`cache_${build_id}_${obj_type}_${id}`);
279
+ if (!doc) return;
280
+ var ret = await db.remove(doc);
281
+ } catch (err) {
282
+ //
283
+ return;
284
+ }
285
+
286
+ return ret;
287
+ };
288
+ export const load_objects_cache = async function (SESSION_ID) {
289
+ var _session = _this.SESSION_OBJ[SESSION_ID];
290
+ if (typeof _this.IS_DOCKER !== 'undefined' || typeof _this.IS_PROCESS_SERVER !== 'undefined') {
291
+ return;
292
+ }
293
+
294
+ const app_id = _session.app_id;
295
+
296
+ const db = await _this.func.utils.connect_pouchdb(SESSION_ID);
297
+ const build_id = _this.APP_OBJ[app_id].app_build_id;
298
+ let ret = await db.find({
299
+ selector: { docType: 'cache_objects', build_id },
300
+ });
301
+
302
+ for (let val of ret.docs) {
303
+ eval(val.obj_type)[_session.app_id][val.id] = val.data;
304
+ }
305
+ };
package/package.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "@xuda.io/xuda-progs-loader-module",
3
+ "version": "1.0.0",
4
+ "main": "index.mjs",
5
+ "type": "module",
6
+ "description": "Auto-generated build for xuda-progs-loader-module.mjs",
7
+ "author": "Auto Publisher"
8
+ }