@xuda.io/xuda-worker-bundle 1.3.2693 → 1.3.2695

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 +339 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -291,7 +291,30 @@ func.runtime.platform.emit = function (name, data) {
291
291
  };
292
292
 
293
293
  // ── Platform helpers for DOM-independent resource loading ──
294
- func.runtime.platform.load_script = function (url, type, callback) {
294
+ func.runtime.platform.apply_element_attributes = function (node, attributes, excluded_keys = []) {
295
+ if (!node?.setAttribute || !attributes) {
296
+ return node;
297
+ }
298
+
299
+ const excluded = new Set(excluded_keys || []);
300
+ const attr_keys = Object.keys(attributes);
301
+ for (let index = 0; index < attr_keys.length; index++) {
302
+ const key = attr_keys[index];
303
+ if (!key || excluded.has(key)) {
304
+ continue;
305
+ }
306
+
307
+ const value = attributes[key];
308
+ if (value === false || typeof value === 'undefined') {
309
+ continue;
310
+ }
311
+
312
+ node.setAttribute(key, value === null ? '' : `${value}`);
313
+ }
314
+
315
+ return node;
316
+ };
317
+ func.runtime.platform.load_script = function (url, type, callback, attributes) {
295
318
  const doc = func.runtime.platform.get_document();
296
319
  if (!doc?.createElement || !doc?.head?.appendChild) {
297
320
  if (callback) {
@@ -299,19 +322,63 @@ func.runtime.platform.load_script = function (url, type, callback) {
299
322
  }
300
323
  return;
301
324
  }
325
+ const find_existing_script = function () {
326
+ const asset_key = attributes?.['data-xuda-asset-key'];
327
+ const scripts = doc.querySelectorAll ? Array.from(doc.querySelectorAll('script')) : [];
328
+ return scripts.find(function (script) {
329
+ if (asset_key && script.getAttribute('data-xuda-asset-key') === asset_key) {
330
+ return true;
331
+ }
332
+ return !!(url && script.getAttribute('src') === url);
333
+ }) || null;
334
+ };
335
+
336
+ const existing_script = find_existing_script();
337
+ if (existing_script) {
338
+ if (callback) {
339
+ if (existing_script.getAttribute('data-xuda-loaded') === 'true' || !url) {
340
+ callback();
341
+ } else {
342
+ existing_script.addEventListener('load', callback, { once: true });
343
+ existing_script.addEventListener('error', callback, { once: true });
344
+ }
345
+ }
346
+ return existing_script;
347
+ }
348
+
302
349
  const script = doc.createElement('script');
303
350
  script.src = url;
304
351
  if (type) script.type = type;
305
- script.onload = callback;
352
+ func.runtime.platform.apply_element_attributes(script, attributes, ['src', 'type']);
353
+ script.onload = function () {
354
+ script.setAttribute('data-xuda-loaded', 'true');
355
+ if (callback) {
356
+ callback();
357
+ }
358
+ };
359
+ script.onerror = function () {
360
+ if (callback) {
361
+ callback();
362
+ }
363
+ };
306
364
  doc.head.appendChild(script);
365
+ return script;
307
366
  };
308
- func.runtime.platform.load_css = function (href) {
367
+ func.runtime.platform.load_css = function (href, attributes) {
309
368
  const doc = func.runtime.platform.get_document();
310
369
  if (!doc?.createElement || !doc?.head) {
311
370
  return;
312
371
  }
313
372
  try {
314
- if (doc.querySelector('link[href="' + href + '"]')) return;
373
+ const asset_key = attributes?.['data-xuda-asset-key'];
374
+ const existing_links = doc.querySelectorAll ? Array.from(doc.querySelectorAll('link')) : [];
375
+ const existing = existing_links.find(function (link) {
376
+ if (asset_key && link.getAttribute('data-xuda-asset-key') === asset_key) {
377
+ return true;
378
+ }
379
+ return !!(href && link.getAttribute('href') === href);
380
+ });
381
+ if (existing) return existing;
315
382
  } catch (err) {
316
383
  return;
317
384
  }
@@ -319,7 +386,9 @@ func.runtime.platform.load_css = function (href) {
319
386
  link.rel = 'stylesheet';
320
387
  link.type = 'text/css';
321
388
  link.href = href;
389
+ func.runtime.platform.apply_element_attributes(link, attributes, ['href']);
322
390
  doc.head.insertBefore(link, doc.head.firstChild);
391
+ return link;
323
392
  };
324
393
  func.runtime.platform.remove_js_css = function (filename, filetype) {
325
394
  const doc = func.runtime.platform.get_document();
@@ -610,6 +679,147 @@ func.runtime.workers.delete_promise = function (SESSION_ID, worker_id, promise_q
610
679
  delete registry_entry.promise_queue[promise_queue_id];
611
680
  return true;
612
681
  };
682
+ func.runtime.render.clone_runtime_options = function (value) {
683
+ if (typeof structuredClone === 'function') {
684
+ try {
685
+ return structuredClone(value);
686
+ } catch (_) {}
687
+ }
688
+
689
+ if (Array.isArray(value)) {
690
+ return value.map(function (item) {
691
+ return func.runtime.render.clone_runtime_options(item);
692
+ });
693
+ }
694
+
695
+ if (value && typeof value === 'object') {
696
+ const cloned = {};
697
+ const keys = Object.keys(value);
698
+ for (let index = 0; index < keys.length; index++) {
699
+ const key = keys[index];
700
+ cloned[key] = func.runtime.render.clone_runtime_options(value[key]);
701
+ }
702
+ return cloned;
703
+ }
704
+
705
+ return value;
706
+ };
707
+ func.runtime.render.normalize_runtime_bootstrap = function (raw_options = {}) {
708
+ const options = raw_options || {};
709
+ let app_computing_mode = options.app_computing_mode || '';
710
+ let app_render_mode = options.app_render_mode || '';
711
+ let app_client_activation = options.app_client_activation || '';
712
+ let ssr_payload = options.ssr_payload || null;
713
+
714
+ if (typeof ssr_payload === 'string') {
715
+ try {
716
+ ssr_payload = JSON.parse(ssr_payload);
717
+ } catch (_) {
718
+ ssr_payload = null;
719
+ }
720
+ }
721
+
722
+ if (ssr_payload && typeof ssr_payload === 'object') {
723
+ ssr_payload = func.runtime.render.clone_runtime_options(ssr_payload);
724
+ }
725
+
726
+ if (!app_computing_mode) {
727
+ if (app_render_mode === 'ssr_first_page' || app_render_mode === 'ssr_full') {
728
+ app_computing_mode = 'server';
729
+ } else {
730
+ app_computing_mode = 'main';
731
+ }
732
+ }
733
+
734
+ switch (app_computing_mode) {
735
+ case 'main':
736
+ app_render_mode = 'csr';
737
+ app_client_activation = 'none';
738
+ break;
739
+
740
+ case 'worker':
741
+ app_render_mode = 'csr';
742
+ app_client_activation = 'none';
743
+ break;
744
+
745
+ default:
746
+ app_computing_mode = 'server';
747
+ if (app_render_mode !== 'ssr_full') {
748
+ app_render_mode = 'ssr_first_page';
749
+ }
750
+ app_client_activation = app_render_mode === 'ssr_full' ? 'hydrate' : 'takeover';
751
+ break;
752
+ }
753
+
754
+ if (ssr_payload && typeof ssr_payload === 'object') {
755
+ if (!ssr_payload.app_render_mode) {
756
+ ssr_payload.app_render_mode = app_render_mode;
757
+ }
758
+ if (!ssr_payload.app_client_activation) {
759
+ ssr_payload.app_client_activation = app_client_activation;
760
+ }
761
+ if (!ssr_payload.app_computing_mode) {
762
+ ssr_payload.app_computing_mode = app_computing_mode;
763
+ }
764
+ }
765
+
766
+ return {
767
+ app_computing_mode,
768
+ app_render_mode,
769
+ app_client_activation,
770
+ ssr_payload,
771
+ };
772
+ };
773
+ func.runtime.render.apply_runtime_bootstrap_defaults = function (target = {}) {
774
+ const normalized = func.runtime.render.normalize_runtime_bootstrap(target);
775
+ target.app_computing_mode = normalized.app_computing_mode;
776
+ target.app_render_mode = normalized.app_render_mode;
777
+ target.app_client_activation = normalized.app_client_activation;
778
+ target.ssr_payload = normalized.ssr_payload;
779
+ return normalized;
780
+ };
781
+ func.runtime.render.is_server_render_mode = function (target = {}) {
782
+ const normalized = func.runtime.render.normalize_runtime_bootstrap(target?.opt || target);
783
+ return normalized.app_computing_mode === 'server' && normalized.app_render_mode !== 'csr';
784
+ };
785
+ func.runtime.render.is_takeover_mode = function (target = {}) {
786
+ const normalized = func.runtime.render.normalize_runtime_bootstrap(target?.opt || target);
787
+ return normalized.app_client_activation === 'takeover';
788
+ };
789
+ func.runtime.render.is_hydration_mode = function (target = {}) {
790
+ const normalized = func.runtime.render.normalize_runtime_bootstrap(target?.opt || target);
791
+ return normalized.app_client_activation === 'hydrate';
792
+ };
793
+ func.runtime.render.get_ssr_payload = function (target = {}) {
794
+ if (target?.opt?.ssr_payload) {
795
+ return target.opt.ssr_payload;
796
+ }
797
+ if (target?.ssr_payload) {
798
+ return target.ssr_payload;
799
+ }
800
+ const win = func.runtime.platform.get_window();
801
+ return win?.__XUDA_SSR__ || null;
802
+ };
803
+ func.runtime.render.should_use_ssr_payload = function (SESSION_ID, paramsP) {
804
+ const session = SESSION_OBJ?.[SESSION_ID];
805
+ const payload = func.runtime.render.get_ssr_payload(session);
806
+ if (!payload || payload._consumed) {
807
+ return false;
808
+ }
809
+ if (paramsP?.prog_id && payload.prog_id && payload.prog_id !== paramsP.prog_id) {
810
+ return false;
811
+ }
812
+ return true;
813
+ };
814
+ func.runtime.render.mark_ssr_payload_consumed = function (SESSION_ID) {
815
+ const session = SESSION_OBJ?.[SESSION_ID];
816
+ const payload = func.runtime.render.get_ssr_payload(session);
817
+ if (!payload || typeof payload !== 'object') {
818
+ return false;
819
+ }
820
+ payload._consumed = true;
821
+ return true;
822
+ };
613
823
  func.runtime.render.get_root_data_system = function (SESSION_ID) {
614
824
  return SESSION_OBJ[SESSION_ID]?.DS_GLB?.[0]?.data_system || null;
615
825
  };
@@ -1013,10 +1223,10 @@ func.runtime.resources.load_cdn = async function (SESSION_ID, resource) {
1013
1223
  await func.utils.load_js_on_demand(normalized_resource.src);
1014
1224
  break;
1015
1225
  case 'css':
1016
- await func.utils.load_js_on_demand(normalized_resource.src);
1226
+ func.runtime.platform.load_css(normalized_resource.src);
1017
1227
  break;
1018
1228
  case 'module':
1019
- func.utils.load_js_on_demand(normalized_resource.src, 'module');
1229
+ await func.utils.load_js_on_demand(normalized_resource.src, 'module');
1020
1230
  break;
1021
1231
  default:
1022
1232
  await func.utils.load_js_on_demand(normalized_resource.src);
@@ -2914,6 +3124,129 @@ func.runtime.render.render_to_string = async function (options = {}) {
2914
3124
  });
2915
3125
 
2916
3126
  return await func.runtime.render.render_tree_to_string(treeP, options);
3127
+ };
3128
+ func.runtime.render.get_server_render_mode = function (options = {}) {
3129
+ const normalized = func.runtime.render.normalize_runtime_bootstrap({
3130
+ app_computing_mode: options.app_computing_mode,
3131
+ app_render_mode: options.app_render_mode,
3132
+ app_client_activation: options.app_client_activation,
3133
+ });
3134
+
3135
+ return normalized;
3136
+ };
3137
+ func.runtime.render.build_server_render_params = async function (options = {}) {
3138
+ const SESSION_ID = options.SESSION_ID;
3139
+ const prog_id = options.prog_id;
3140
+ const dsSessionP = options.dsSessionP;
3141
+ const _session = SESSION_OBJ?.[SESSION_ID] || {};
3142
+ const _ds = _session?.DS_GLB?.[dsSessionP] || {};
3143
+ const viewDoc = options.viewDoc || (await func.utils?.VIEWS_OBJ?.get?.(SESSION_ID, prog_id));
3144
+
3145
+ if (!viewDoc?.properties) {
3146
+ throw new Error(`view document not found for ${prog_id}`);
3147
+ }
3148
+
3149
+ const base_params = _ds?.screen_params ? func.runtime.render.safe_clone_tree_value(_ds.screen_params) : {};
3150
+ const screenId = options.screenId || base_params.screenId || `ssr_${prog_id}_${dsSessionP || '0'}`;
3151
+ const paramsP = {
3152
+ ...base_params,
3153
+ prog_id,
3154
+ sourceScreenP: null,
3155
+ $callingContainerP: null,
3156
+ triggerIdP: null,
3157
+ callingDataSource_objP: _ds,
3158
+ rowIdP: typeof options.rowIdP !== 'undefined' ? options.rowIdP : (_ds?.currentRecordId || null),
3159
+ renderType: viewDoc.properties?.renderType,
3160
+ parameters_obj_inP: options.parameters_obj_inP || base_params.parameters_obj_inP || options.parameters_raw_obj || {},
3161
+ source_functionP: options.source_functionP || base_params.source_functionP || 'render_string',
3162
+ is_panelP: false,
3163
+ screen_type: options.screen_type || base_params.screen_type || 'render_string',
3164
+ screenInfo: viewDoc,
3165
+ call_screen_propertiesP: base_params.call_screen_propertiesP,
3166
+ parentDataSourceNoP: typeof _ds?.parentDataSourceNo === 'undefined' || _ds?.parentDataSourceNo === null ? 0 : _ds.parentDataSourceNo,
3167
+ parameters_raw_obj: options.parameters_raw_obj || base_params.parameters_raw_obj || {},
3168
+ dsSessionP,
3169
+ screenId,
3170
+ containerIdP: base_params.containerIdP || `ssr_container_${screenId}`,
3171
+ };
3172
+
3173
+ if (_ds) {
3174
+ _ds.screen_params = paramsP;
3175
+ }
3176
+
3177
+ return paramsP;
3178
+ };
3179
+ func.runtime.render.build_prog_tree = async function (options = {}) {
3180
+ const SESSION_ID = options.SESSION_ID;
3181
+ const prog_id = options.prog_id;
3182
+ const viewDoc = options.viewDoc || (await func.utils?.VIEWS_OBJ?.get?.(SESSION_ID, prog_id));
3183
+
3184
+ if (!viewDoc?.progUi?.length) {
3185
+ throw new Error(`progUi not found for ${prog_id}`);
3186
+ }
3187
+
3188
+ const paramsP = options.paramsP || (await func.runtime.render.build_server_render_params({
3189
+ ...options,
3190
+ SESSION_ID,
3191
+ prog_id,
3192
+ viewDoc,
3193
+ }));
3194
+ const root_index = typeof options.root_index === 'number' ? options.root_index : 0;
3195
+ const root_node = func.runtime.render.safe_clone_tree_value(viewDoc.progUi[root_index]);
3196
+ const tree = await func.runtime.render.build_tree({
3197
+ SESSION_ID,
3198
+ nodeP: root_node,
3199
+ paramsP,
3200
+ });
3201
+
3202
+ return {
3203
+ tree,
3204
+ paramsP,
3205
+ viewDoc,
3206
+ };
3207
+ };
3208
+ func.runtime.render.build_ssr_payload = function (render_program, options = {}) {
3209
+ const runtime_profile = func.runtime.render.get_server_render_mode(options);
3210
+ return {
3211
+ contract: 'xuda.ssr.v1',
3212
+ prog_id: options.prog_id,
3213
+ screenId: render_program.paramsP.screenId,
3214
+ containerId: render_program.paramsP.containerIdP,
3215
+ app_computing_mode: runtime_profile.app_computing_mode,
3216
+ app_render_mode: runtime_profile.app_render_mode,
3217
+ app_client_activation: runtime_profile.app_client_activation,
3218
+ tree_contract: func.runtime.render.TREE_CONTRACT_VERSION,
3219
+ };
3220
+ };
3221
+ func.runtime.render.build_ssr_screen_html = function (html, render_program, options = {}) {
3222
+ const payload = func.runtime.render.build_ssr_payload(render_program, options);
3223
+ const screenId = func.runtime.render.escape_html_attribute(payload.screenId || '');
3224
+ const containerId = func.runtime.render.escape_html_attribute(payload.containerId || '');
3225
+ const activation = func.runtime.render.escape_html_attribute(payload.app_client_activation || 'takeover');
3226
+
3227
+ return `<div data-xuda-ssr-embed="true" class="xu_embed_div"><div id="${screenId}" class="xu_embed_container" data-xuda-ssr-screen="true" data-xuda-ssr-screen-id="${screenId}" data-xuda-activation="${activation}" style="display: contents;"><div id="${containerId}" data-xuda-ssr-root-frame="true" data-xuda-ssr-screen-id="${screenId}" data-xuda-activation="${activation}" style="display: contents;">${html}</div></div></div>`;
3228
+ };
3229
+ func.runtime.render.render_prog_to_string = async function (options = {}) {
3230
+ const render_program = await func.runtime.render.build_prog_tree(options);
3231
+ const html = await func.runtime.render.render_to_string({
3232
+ ...options,
3233
+ SESSION_ID: options.SESSION_ID,
3234
+ treeP: render_program.tree,
3235
+ paramsP: render_program.paramsP,
3236
+ });
3237
+ const ssr_payload = func.runtime.render.build_ssr_payload(render_program, options);
3238
+ const screen_html = func.runtime.render.build_ssr_screen_html(html, render_program, options);
3239
+
3240
+ return {
3241
+ prog_id: options.prog_id,
3242
+ dsSessionP: render_program.paramsP.dsSessionP,
3243
+ screenId: render_program.paramsP.screenId,
3244
+ html,
3245
+ screen_html,
3246
+ tree_json: func.runtime.render.serialize_tree(render_program.tree),
3247
+ paramsP: render_program.paramsP,
3248
+ ssr_payload,
3249
+ };
2917
3250
  };
2918
3251
  glb.DEBUG_INFO_OBJ = {};
2919
3252
  // var CONNECTION_ATTEMPTS = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/xuda-worker-bundle",
3
- "version": "1.3.2693",
3
+ "version": "1.3.2695",
4
4
  "description": "xuda framework",
5
5
  "main": "index.js",
6
6
  "scripts": {