@xuda.io/runtime-bundle 1.0.1436 → 1.0.1438

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.
@@ -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();
@@ -353,6 +422,128 @@ func.runtime.platform.set_cursor = function (element, cursor) {
353
422
  node.style.cursor = cursor;
354
423
  }
355
424
  };
425
+ func.runtime.program.normalize_doc_for_runtime = function (doc) {
426
+ if (!doc || doc.__xudaRuntimeNormalized || !Array.isArray(doc.progUi) || !doc.progUi.length) {
427
+ return doc;
428
+ }
429
+
430
+ const normalize_tag_name = function (tag_name) {
431
+ return `${tag_name || ''}`.trim().toLowerCase();
432
+ };
433
+ const merge_attributes = function (target, source) {
434
+ const merged = { ...(target || {}) };
435
+ const source_attributes = source || {};
436
+ const keys = Object.keys(source_attributes);
437
+
438
+ for (let index = 0; index < keys.length; index++) {
439
+ const key = keys[index];
440
+ const value = source_attributes[key];
441
+ if (typeof value === 'undefined') {
442
+ continue;
443
+ }
444
+
445
+ if (key === 'class' && merged.class && value) {
446
+ const next_value = `${merged.class} ${value}`.trim();
447
+ merged.class = Array.from(new Set(next_value.split(/\s+/).filter(Boolean))).join(' ');
448
+ continue;
449
+ }
450
+
451
+ if (key === 'style' && merged.style && value) {
452
+ merged.style = `${merged.style}; ${value}`.trim();
453
+ continue;
454
+ }
455
+
456
+ if (typeof merged[key] === 'undefined') {
457
+ merged[key] = value;
458
+ }
459
+ }
460
+
461
+ return merged;
462
+ };
463
+ const normalize_nodes = function (nodes, state) {
464
+ const normalized_nodes = [];
465
+
466
+ for (let index = 0; index < (nodes || []).length; index++) {
467
+ const node = nodes[index];
468
+ if (!node || typeof node !== 'object') {
469
+ continue;
470
+ }
471
+
472
+ const tag_name = normalize_tag_name(node.tagName);
473
+
474
+ if (tag_name === '!doctype') {
475
+ state.changed = true;
476
+ continue;
477
+ }
478
+
479
+ if (tag_name === 'html') {
480
+ state.changed = true;
481
+ state.root_attributes = merge_attributes(state.root_attributes, node.attributes);
482
+ normalized_nodes.push.apply(normalized_nodes, normalize_nodes(node.children, state));
483
+ continue;
484
+ }
485
+
486
+ if (tag_name === 'head') {
487
+ state.changed = true;
488
+ normalized_nodes.push.apply(normalized_nodes, normalize_nodes(node.children, state));
489
+ continue;
490
+ }
491
+
492
+ if (tag_name === 'body') {
493
+ state.changed = true;
494
+ state.root_attributes = merge_attributes(state.root_attributes, node.attributes);
495
+ normalized_nodes.push.apply(normalized_nodes, normalize_nodes(node.children, state));
496
+ continue;
497
+ }
498
+
499
+ let next_node = node;
500
+ if (Array.isArray(node.children) && node.children.length) {
501
+ const next_children = normalize_nodes(node.children, state);
502
+ if (next_children !== node.children) {
503
+ next_node = {
504
+ ...node,
505
+ children: next_children,
506
+ };
507
+ state.changed = true;
508
+ }
509
+ }
510
+
511
+ normalized_nodes.push(next_node);
512
+ }
513
+
514
+ return normalized_nodes;
515
+ };
516
+
517
+ const [root_node, ...extra_nodes] = doc.progUi;
518
+ if (!root_node || typeof root_node !== 'object') {
519
+ return doc;
520
+ }
521
+
522
+ const state = {
523
+ changed: false,
524
+ root_attributes: {},
525
+ };
526
+
527
+ const normalized_children = normalize_nodes([...(root_node.children || []), ...extra_nodes], state);
528
+ const merged_attributes = merge_attributes(root_node.attributes, state.root_attributes);
529
+
530
+ if (!state.changed && !Object.keys(state.root_attributes).length) {
531
+ doc.__xudaRuntimeNormalized = true;
532
+ return doc;
533
+ }
534
+
535
+ return {
536
+ ...doc,
537
+ __xudaRuntimeNormalized: true,
538
+ progUi: [
539
+ {
540
+ ...root_node,
541
+ attributes: merged_attributes,
542
+ children: normalized_children,
543
+ },
544
+ ],
545
+ };
546
+ };
356
547
 
357
548
  func.runtime.env = {
358
549
  get_url_params: function () {
@@ -610,6 +801,147 @@ func.runtime.workers.delete_promise = function (SESSION_ID, worker_id, promise_q
610
801
  delete registry_entry.promise_queue[promise_queue_id];
611
802
  return true;
612
803
  };
804
+ func.runtime.render.clone_runtime_options = function (value) {
805
+ if (typeof structuredClone === 'function') {
806
+ try {
807
+ return structuredClone(value);
808
+ } catch (_) {}
809
+ }
810
+
811
+ if (Array.isArray(value)) {
812
+ return value.map(function (item) {
813
+ return func.runtime.render.clone_runtime_options(item);
814
+ });
815
+ }
816
+
817
+ if (value && typeof value === 'object') {
818
+ const cloned = {};
819
+ const keys = Object.keys(value);
820
+ for (let index = 0; index < keys.length; index++) {
821
+ const key = keys[index];
822
+ cloned[key] = func.runtime.render.clone_runtime_options(value[key]);
823
+ }
824
+ return cloned;
825
+ }
826
+
827
+ return value;
828
+ };
829
+ func.runtime.render.normalize_runtime_bootstrap = function (raw_options = {}) {
830
+ const options = raw_options || {};
831
+ let app_computing_mode = options.app_computing_mode || '';
832
+ let app_render_mode = options.app_render_mode || '';
833
+ let app_client_activation = options.app_client_activation || '';
834
+ let ssr_payload = options.ssr_payload || null;
835
+
836
+ if (typeof ssr_payload === 'string') {
837
+ try {
838
+ ssr_payload = JSON.parse(ssr_payload);
839
+ } catch (_) {
840
+ ssr_payload = null;
841
+ }
842
+ }
843
+
844
+ if (ssr_payload && typeof ssr_payload === 'object') {
845
+ ssr_payload = func.runtime.render.clone_runtime_options(ssr_payload);
846
+ }
847
+
848
+ if (!app_computing_mode) {
849
+ if (app_render_mode === 'ssr_first_page' || app_render_mode === 'ssr_full') {
850
+ app_computing_mode = 'server';
851
+ } else {
852
+ app_computing_mode = 'main';
853
+ }
854
+ }
855
+
856
+ switch (app_computing_mode) {
857
+ case 'main':
858
+ app_render_mode = 'csr';
859
+ app_client_activation = 'none';
860
+ break;
861
+
862
+ case 'worker':
863
+ app_render_mode = 'csr';
864
+ app_client_activation = 'none';
865
+ break;
866
+
867
+ default:
868
+ app_computing_mode = 'server';
869
+ if (app_render_mode !== 'ssr_full') {
870
+ app_render_mode = 'ssr_first_page';
871
+ }
872
+ app_client_activation = app_render_mode === 'ssr_full' ? 'hydrate' : 'takeover';
873
+ break;
874
+ }
875
+
876
+ if (ssr_payload && typeof ssr_payload === 'object') {
877
+ if (!ssr_payload.app_render_mode) {
878
+ ssr_payload.app_render_mode = app_render_mode;
879
+ }
880
+ if (!ssr_payload.app_client_activation) {
881
+ ssr_payload.app_client_activation = app_client_activation;
882
+ }
883
+ if (!ssr_payload.app_computing_mode) {
884
+ ssr_payload.app_computing_mode = app_computing_mode;
885
+ }
886
+ }
887
+
888
+ return {
889
+ app_computing_mode,
890
+ app_render_mode,
891
+ app_client_activation,
892
+ ssr_payload,
893
+ };
894
+ };
895
+ func.runtime.render.apply_runtime_bootstrap_defaults = function (target = {}) {
896
+ const normalized = func.runtime.render.normalize_runtime_bootstrap(target);
897
+ target.app_computing_mode = normalized.app_computing_mode;
898
+ target.app_render_mode = normalized.app_render_mode;
899
+ target.app_client_activation = normalized.app_client_activation;
900
+ target.ssr_payload = normalized.ssr_payload;
901
+ return normalized;
902
+ };
903
+ func.runtime.render.is_server_render_mode = function (target = {}) {
904
+ const normalized = func.runtime.render.normalize_runtime_bootstrap(target?.opt || target);
905
+ return normalized.app_computing_mode === 'server' && normalized.app_render_mode !== 'csr';
906
+ };
907
+ func.runtime.render.is_takeover_mode = function (target = {}) {
908
+ const normalized = func.runtime.render.normalize_runtime_bootstrap(target?.opt || target);
909
+ return normalized.app_client_activation === 'takeover';
910
+ };
911
+ func.runtime.render.is_hydration_mode = function (target = {}) {
912
+ const normalized = func.runtime.render.normalize_runtime_bootstrap(target?.opt || target);
913
+ return normalized.app_client_activation === 'hydrate';
914
+ };
915
+ func.runtime.render.get_ssr_payload = function (target = {}) {
916
+ if (target?.opt?.ssr_payload) {
917
+ return target.opt.ssr_payload;
918
+ }
919
+ if (target?.ssr_payload) {
920
+ return target.ssr_payload;
921
+ }
922
+ const win = func.runtime.platform.get_window();
923
+ return win?.__XUDA_SSR__ || null;
924
+ };
925
+ func.runtime.render.should_use_ssr_payload = function (SESSION_ID, paramsP) {
926
+ const session = SESSION_OBJ?.[SESSION_ID];
927
+ const payload = func.runtime.render.get_ssr_payload(session);
928
+ if (!payload || payload._consumed) {
929
+ return false;
930
+ }
931
+ if (paramsP?.prog_id && payload.prog_id && payload.prog_id !== paramsP.prog_id) {
932
+ return false;
933
+ }
934
+ return true;
935
+ };
936
+ func.runtime.render.mark_ssr_payload_consumed = function (SESSION_ID) {
937
+ const session = SESSION_OBJ?.[SESSION_ID];
938
+ const payload = func.runtime.render.get_ssr_payload(session);
939
+ if (!payload || typeof payload !== 'object') {
940
+ return false;
941
+ }
942
+ payload._consumed = true;
943
+ return true;
944
+ };
613
945
  func.runtime.render.get_root_data_system = function (SESSION_ID) {
614
946
  return SESSION_OBJ[SESSION_ID]?.DS_GLB?.[0]?.data_system || null;
615
947
  };
@@ -1013,10 +1345,10 @@ func.runtime.resources.load_cdn = async function (SESSION_ID, resource) {
1013
1345
  await func.utils.load_js_on_demand(normalized_resource.src);
1014
1346
  break;
1015
1347
  case 'css':
1016
- await func.utils.load_js_on_demand(normalized_resource.src);
1348
+ func.runtime.platform.load_css(normalized_resource.src);
1017
1349
  break;
1018
1350
  case 'module':
1019
- func.utils.load_js_on_demand(normalized_resource.src, 'module');
1351
+ await func.utils.load_js_on_demand(normalized_resource.src, 'module');
1020
1352
  break;
1021
1353
  default:
1022
1354
  await func.utils.load_js_on_demand(normalized_resource.src);