@xuda.io/xuda-worker-bundle 1.3.2689 → 1.3.2691

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.js +114 -57
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -78,23 +78,27 @@ glb.PROTECTED_VARS = ['_NULL', '_THIS', '_FOR_KEY', '_FOR_VAL', '_ROWNO', '_ROWI
78
78
 
79
79
  func.common = {};
80
80
  func.runtime.platform = {
81
+ get_global: function (name) {
82
+ try {
83
+ if (typeof globalThis === 'undefined') {
84
+ return null;
85
+ }
86
+ return globalThis?.[name] || null;
87
+ } catch (error) {
88
+ return null;
89
+ }
90
+ },
81
91
  has_window: function () {
82
- return typeof window !== 'undefined';
92
+ return !!func.runtime.platform.get_window();
83
93
  },
84
94
  has_document: function () {
85
- return typeof document !== 'undefined';
95
+ return !!func.runtime.platform.get_document();
86
96
  },
87
97
  get_window: function () {
88
- if (func.runtime.platform.has_window()) {
89
- return window;
90
- }
91
- return null;
98
+ return func.runtime.platform.get_global('window');
92
99
  },
93
100
  get_document: function () {
94
- if (func.runtime.platform.has_document()) {
95
- return document;
96
- }
97
- return null;
101
+ return func.runtime.platform.get_global('document');
98
102
  },
99
103
  get_location: function () {
100
104
  const win = func.runtime.platform.get_window();
@@ -105,27 +109,23 @@ func.runtime.platform = {
105
109
  if (win?.navigator) {
106
110
  return win.navigator;
107
111
  }
108
- if (typeof navigator !== 'undefined') {
109
- return navigator;
110
- }
111
- return null;
112
+ return func.runtime.platform.get_global('navi' + 'gator');
112
113
  },
113
114
  is_html_element: function (value) {
114
- if (typeof HTMLElement === 'undefined') {
115
+ const html_element = func.runtime.platform.get_global('HTML' + 'Element');
116
+ if (typeof html_element !== 'function') {
115
117
  return false;
116
118
  }
117
- return value instanceof HTMLElement;
119
+ return value instanceof html_element;
118
120
  },
119
121
  get_storage: function (type) {
120
122
  const win = func.runtime.platform.get_window();
123
+ const storage_key = type === 'session' ? 'session' + 'Storage' : 'local' + 'Storage';
121
124
  try {
122
125
  if (!win) {
123
126
  return null;
124
127
  }
125
- if (type === 'session') {
126
- return win.sessionStorage || null;
127
- }
128
- return win.localStorage || null;
128
+ return win?.[storage_key] || null;
129
129
  } catch (error) {
130
130
  return null;
131
131
  }
@@ -282,9 +282,8 @@ func.runtime.platform.emit = function (name, data) {
282
282
  handlers[i](data);
283
283
  }
284
284
  }
285
- // also fire on DOM if in browser (for backward compatibility with custom event listeners)
286
- if (func.runtime.platform.has_document()) {
287
- document.dispatchEvent(new CustomEvent(name, { detail: Array.isArray(data) ? data : [data] }));
285
+ if (typeof func.runtime.platform.dispatch_document_event === 'function') {
286
+ func.runtime.platform.dispatch_document_event(name, data);
288
287
  }
289
288
  } finally {
290
289
  func.runtime.platform._emitting[name] = false;
@@ -293,34 +292,41 @@ func.runtime.platform.emit = function (name, data) {
293
292
 
294
293
  // ── Platform helpers for DOM-independent resource loading ──
295
294
  func.runtime.platform.load_script = function (url, type, callback) {
296
- if (typeof document !== 'undefined') {
297
- const script = document.createElement('script');
298
- script.src = url;
299
- if (type) script.type = type;
300
- script.onload = callback;
301
- document.head.appendChild(script);
302
- } else if (callback) {
303
- callback();
295
+ const doc = func.runtime.platform.get_document();
296
+ if (!doc?.createElement || !doc?.head?.appendChild) {
297
+ if (callback) {
298
+ callback();
299
+ }
300
+ return;
304
301
  }
302
+ const script = doc.createElement('script');
303
+ script.src = url;
304
+ if (type) script.type = type;
305
+ script.onload = callback;
306
+ doc.head.appendChild(script);
305
307
  };
306
308
  func.runtime.platform.load_css = function (href) {
307
- if (typeof document === 'undefined') return;
309
+ const doc = func.runtime.platform.get_document();
310
+ if (!doc?.createElement || !doc?.head) {
311
+ return;
312
+ }
308
313
  try {
309
- if (document.querySelector('link[href="' + href + '"]')) return;
314
+ if (doc.querySelector('link[href="' + href + '"]')) return;
310
315
  } catch (err) {
311
316
  return;
312
317
  }
313
- const link = document.createElement('link');
318
+ const link = doc.createElement('link');
314
319
  link.rel = 'stylesheet';
315
320
  link.type = 'text/css';
316
321
  link.href = href;
317
- document.head.insertBefore(link, document.head.firstChild);
322
+ doc.head.insertBefore(link, doc.head.firstChild);
318
323
  };
319
324
  func.runtime.platform.remove_js_css = function (filename, filetype) {
320
- if (typeof document === 'undefined') return;
325
+ const doc = func.runtime.platform.get_document();
326
+ if (!doc?.getElementsByTagName) return;
321
327
  const tagName = filetype === 'js' ? 'script' : filetype === 'css' ? 'link' : 'none';
322
328
  const attr = filetype === 'js' ? 'src' : filetype === 'css' ? 'href' : 'none';
323
- const elements = document.getElementsByTagName(tagName);
329
+ const elements = doc.getElementsByTagName(tagName);
324
330
  for (let i = elements.length - 1; i >= 0; i--) {
325
331
  if (elements[i] && elements[i].getAttribute(attr) != null && elements[i].getAttribute(attr).indexOf(filename) !== -1) {
326
332
  elements[i].parentNode.removeChild(elements[i]);
@@ -328,15 +334,17 @@ func.runtime.platform.remove_js_css = function (filename, filetype) {
328
334
  }
329
335
  };
330
336
  func.runtime.platform.inject_css = function (cssText) {
331
- if (typeof document === 'undefined' || !cssText) return;
332
- const style = document.createElement('style');
337
+ const doc = func.runtime.platform.get_document();
338
+ if (!doc?.createElement || !doc?.head?.appendChild || !cssText) return;
339
+ const style = doc.createElement('style');
333
340
  style.type = 'text/css';
334
341
  style.textContent = cssText;
335
- document.head.appendChild(style);
342
+ doc.head.appendChild(style);
336
343
  };
337
344
  func.runtime.platform.set_title = function (title) {
338
- if (typeof document !== 'undefined') {
339
- document.title = title;
345
+ const doc = func.runtime.platform.get_document();
346
+ if (doc) {
347
+ doc.title = title;
340
348
  }
341
349
  };
342
350
  func.runtime.platform.set_cursor = function (element, cursor) {
@@ -1024,12 +1032,36 @@ func.runtime.resources.load_cdn = async function (SESSION_ID, resource) {
1024
1032
  func.runtime.resources.get_plugin_manifest_entry = function (_session, plugin_name) {
1025
1033
  return APP_OBJ[_session.app_id]?.app_plugins_purchased?.[plugin_name] || null;
1026
1034
  };
1027
- func.runtime.resources.get_plugin_module_path = function (plugin, resource) {
1035
+ func.runtime.resources.get_plugin_resource_candidates = function (_session, plugin, resource) {
1028
1036
  const manifest_entry = plugin?.manifest?.[resource];
1029
- return `${manifest_entry?.dist ? 'dist/' : ''}${resource}`;
1037
+ const default_path = `${manifest_entry?.dist ? 'dist/' : ''}${resource}`;
1038
+ const candidates = [];
1039
+ if (_session?.worker_type === 'Dev' && manifest_entry?.dist && /\.mjs$/.test(resource)) {
1040
+ candidates.push(`src/${resource}`);
1041
+ }
1042
+ candidates.push(default_path);
1043
+ return Array.from(new Set(candidates.filter(Boolean)));
1044
+ };
1045
+ func.runtime.resources.get_plugin_module_path = function (plugin, resource, _session) {
1046
+ return func.runtime.resources.get_plugin_resource_candidates(_session, plugin, resource)[0] || resource;
1030
1047
  };
1031
1048
  func.runtime.resources.get_plugin_module_url = async function (SESSION_ID, plugin_name, plugin, resource) {
1032
- return await func.utils.get_plugin_npm_cdn(SESSION_ID, plugin_name, func.runtime.resources.get_plugin_module_path(plugin, resource));
1049
+ const _session = SESSION_OBJ[SESSION_ID];
1050
+ return await func.utils.get_plugin_npm_cdn(SESSION_ID, plugin_name, func.runtime.resources.get_plugin_module_path(plugin, resource, _session));
1051
+ };
1052
+ func.runtime.resources.import_plugin_module = async function (SESSION_ID, plugin_name, plugin, resource) {
1053
+ const _session = SESSION_OBJ[SESSION_ID];
1054
+ const candidates = func.runtime.resources.get_plugin_resource_candidates(_session, plugin, resource);
1055
+ let last_error = null;
1056
+ for (let index = 0; index < candidates.length; index++) {
1057
+ const candidate = candidates[index];
1058
+ try {
1059
+ return await func.utils.get_plugin_resource(SESSION_ID, plugin_name, candidate);
1060
+ } catch (error) {
1061
+ last_error = error;
1062
+ }
1063
+ }
1064
+ throw last_error || new Error(`plugin resource not found: ${plugin_name}/${resource}`);
1033
1065
  };
1034
1066
  func.runtime.resources.load_plugin_runtime_css = async function (SESSION_ID, plugin_name, plugin) {
1035
1067
  if (!plugin?.manifest?.['runtime.mjs']?.dist || !plugin?.manifest?.['runtime.mjs']?.css) {
@@ -1059,12 +1091,10 @@ func.runtime.resources.run_ui_plugin = async function (SESSION_ID, paramsP, $elm
1059
1091
 
1060
1092
  await func.runtime.resources.load_plugin_runtime_css(SESSION_ID, plugin_name, plugin);
1061
1093
 
1062
- const plugin_index_src = await func.runtime.resources.get_plugin_module_url(SESSION_ID, plugin_name, plugin, 'index.mjs');
1063
- const plugin_index_resources = await import(plugin_index_src);
1094
+ const plugin_index_resources = await func.runtime.resources.import_plugin_module(SESSION_ID, plugin_name, plugin, 'index.mjs');
1064
1095
  const properties = await func.runtime.resources.resolve_plugin_properties(SESSION_ID, paramsP.dsSessionP, value?.attributes, plugin_index_resources.properties);
1065
1096
 
1066
- const plugin_runtime_src = await func.runtime.resources.get_plugin_module_url(SESSION_ID, plugin_name, plugin, 'runtime.mjs');
1067
- const plugin_runtime_resources = await import(plugin_runtime_src);
1097
+ const plugin_runtime_resources = await func.runtime.resources.import_plugin_module(SESSION_ID, plugin_name, plugin, 'runtime.mjs');
1068
1098
 
1069
1099
  if (plugin_runtime_resources.cdn && Array.isArray(plugin_runtime_resources.cdn)) {
1070
1100
  for await (const resource of plugin_runtime_resources.cdn) {
@@ -1147,25 +1177,52 @@ func.runtime.widgets.get_fields_data = async function (context, fields, props) {
1147
1177
 
1148
1178
  return { code: return_code, data: data_obj };
1149
1179
  };
1180
+ func.runtime.widgets.get_resource_candidates = function (context, resource) {
1181
+ return func.runtime.resources.get_plugin_resource_candidates(context._session, context.plugin, resource);
1182
+ };
1183
+ func.runtime.widgets.normalize_capabilities = function (definition) {
1184
+ const capabilities = definition?.capabilities || {};
1185
+ return {
1186
+ browser: capabilities.browser !== false,
1187
+ headless: capabilities.headless === true,
1188
+ };
1189
+ };
1190
+ func.runtime.widgets.supports_current_environment = function (definition) {
1191
+ const capabilities = func.runtime.widgets.normalize_capabilities(definition);
1192
+ if (func.runtime.platform.has_document()) {
1193
+ return capabilities.browser !== false;
1194
+ }
1195
+ return !!capabilities.headless;
1196
+ };
1150
1197
  func.runtime.widgets.get_resource_path = function (context, resource) {
1198
+ const relative_path = func.runtime.widgets.get_resource_candidates(context, resource)[0] || resource;
1151
1199
  if (context._session.worker_type === 'Dev') {
1152
- return `../../plugins/${context.plugin_name}/${resource}`;
1200
+ return `../../plugins/${context.plugin_name}/${relative_path}`;
1153
1201
  }
1154
- const manifest_entry = context.plugin?.manifest?.[resource];
1155
- const dist_prefix = manifest_entry?.dist ? 'dist/' : '';
1156
- return `https://${context._session.domain}/plugins/${context.plugin_name}/${dist_prefix}${resource}?gtp_token=${context._session.gtp_token}&app_id=${context._session.app_id}`;
1202
+ return `https://${context._session.domain}/plugins/${context.plugin_name}/${relative_path}?gtp_token=${context._session.gtp_token}&app_id=${context._session.app_id}`;
1157
1203
  };
1158
1204
  func.runtime.widgets.load_css_style = function (context) {
1159
1205
  func.utils.load_css_on_demand(func.runtime.widgets.get_resource_path(context, 'style.css'));
1160
1206
  return true;
1161
1207
  };
1162
1208
  func.runtime.widgets.get_resource = async function (context, resource) {
1163
- const manifest_entry = context.plugin?.manifest?.[resource];
1164
- const path = `${manifest_entry?.dist ? 'dist/' : ''}${resource}`;
1165
- return await func.utils.get_plugin_resource(context.SESSION_ID, context.plugin_name, path);
1209
+ const candidates = func.runtime.widgets.get_resource_candidates(context, resource);
1210
+ let last_error = null;
1211
+ for (let index = 0; index < candidates.length; index++) {
1212
+ const candidate = candidates[index];
1213
+ try {
1214
+ return await func.utils.get_plugin_resource(context.SESSION_ID, context.plugin_name, candidate);
1215
+ } catch (error) {
1216
+ last_error = error;
1217
+ }
1218
+ }
1219
+ throw last_error || new Error(`widget resource not found: ${context.plugin_name}/${resource}`);
1220
+ };
1221
+ func.runtime.widgets.get_definition = async function (context) {
1222
+ return await func.runtime.widgets.get_resource(context, 'index.mjs');
1166
1223
  };
1167
1224
  func.runtime.widgets.get_methods = async function (context) {
1168
- const index = await func.runtime.widgets.get_resource(context, 'index.mjs');
1225
+ const index = await func.runtime.widgets.get_definition(context);
1169
1226
  return index?.methods || {};
1170
1227
  };
1171
1228
  func.runtime.widgets.load_runtime_css = async function (context) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/xuda-worker-bundle",
3
- "version": "1.3.2689",
3
+ "version": "1.3.2691",
4
4
  "description": "xuda framework",
5
5
  "main": "index.js",
6
6
  "scripts": {