@yuneta/gobj-js 7.26.27 → 7.26.29

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.
@@ -1950,6 +1950,31 @@ var gobjJs = (function(exports) {
1950
1950
  }
1951
1951
  }
1952
1952
  /************************************************************
1953
+ * The SDF_REQUIRED attributes with no value, or null when there
1954
+ * is none: what is empty is asked type by type, as in C.
1955
+ ************************************************************/
1956
+ function gobj_check_required_attrs(gobj) {
1957
+ const hs = gobj_hsdata(gobj);
1958
+ const missing = [];
1959
+ if (!is_array(gobj.gclass.attrs_table)) return null;
1960
+ for (const it of gobj.gclass.attrs_table) {
1961
+ if (!it.name || !(it.flag & sdata_flag_t.SDF_REQUIRED)) continue;
1962
+ const value = hs[it.name];
1963
+ switch (it.type) {
1964
+ case data_type_t.DTP_STRING:
1965
+ if (!is_string(value) || empty_string(value)) missing.push(it.name);
1966
+ break;
1967
+ case data_type_t.DTP_LIST:
1968
+ case data_type_t.DTP_DICT:
1969
+ case data_type_t.DTP_JSON:
1970
+ if (empty_json(value)) missing.push(it.name);
1971
+ break;
1972
+ case data_type_t.DTP_POINTER: if (!value) missing.push(it.name);
1973
+ }
1974
+ }
1975
+ return missing.length ? missing : null;
1976
+ }
1977
+ /************************************************************
1953
1978
  *
1954
1979
  ************************************************************/
1955
1980
  function gobj_start(gobj) {
@@ -1965,6 +1990,11 @@ var gobjJs = (function(exports) {
1965
1990
  log_error(`${gobj_short_name(gobj)}: GObj DISABLED`);
1966
1991
  return -1;
1967
1992
  }
1993
+ const required_attrs = gobj_check_required_attrs(gobj);
1994
+ if (required_attrs) {
1995
+ log_error(`${gobj_short_name(gobj)}: Cannot start without all required attributes: ` + JSON.stringify(required_attrs));
1996
+ return -1;
1997
+ }
1968
1998
  if (__trace_gobj_start_stop__(gobj)) trace_machine(sprintf("⏺ ⏺ start: %s", gobj_full_name(gobj)));
1969
1999
  gobj.running = true;
1970
2000
  let ret = 0;
@@ -2122,7 +2152,8 @@ var gobjJs = (function(exports) {
2122
2152
  if (!gobj_is_running(gobj)) {
2123
2153
  if (!(gobj.gclass.gclass_flag & gclass_flag_t.gcflag_required_start_to_play)) {
2124
2154
  log_warning(`${gobj_short_name(gobj)}: GObj playing without previous start`);
2125
- gobj_start(gobj);
2155
+ if (start_destroys_it(gobj)) return -1;
2156
+ if (!gobj.running) return -1;
2126
2157
  } else {
2127
2158
  log_error(`${gobj_short_name(gobj)}: Cannot play, start not done`);
2128
2159
  return -1;
@@ -2351,7 +2382,7 @@ var gobjJs = (function(exports) {
2351
2382
  log_error(`gobj NULL, destroying, or bad type`);
2352
2383
  return 0;
2353
2384
  }
2354
- return rc_walk_by_list(gobj.dl_children, walk_type, cb_walking, user_data, user_data2);
2385
+ return rc_walk_by_list(gobj, /* @__PURE__ */ new Set(), walk_type, cb_walking, user_data, user_data2);
2355
2386
  }
2356
2387
  /***************************************************************************
2357
2388
  *
@@ -2361,7 +2392,7 @@ var gobjJs = (function(exports) {
2361
2392
  log_error(`gobj NULL, destroying, or bad type`);
2362
2393
  return 0;
2363
2394
  }
2364
- return rc_walk_by_tree(gobj.dl_children, walk_type, cb_walking, user_data, user_data2);
2395
+ return rc_walk_by_tree(gobj, /* @__PURE__ */ new Set(), walk_type, cb_walking, user_data, user_data2);
2365
2396
  }
2366
2397
  /***************************************************************
2367
2398
  * Walking
@@ -2388,18 +2419,33 @@ var gobjJs = (function(exports) {
2388
2419
  log_error(`walk callback must return a NUMBER (-1 stop, 0 descend, >0 skip children), got ${typeof ret}: taken as 0`);
2389
2420
  return 0;
2390
2421
  }
2391
- function rc_walk_by_list(iter_, walk_type, cb_walking, user_data, user_data2) {
2392
- let iter = [...iter_];
2422
+ /***************************************************************
2423
+ * Whether a walk steps over this gobj of the list of `owner`.
2424
+ *
2425
+ * Each list is walked over a COPY of it, because a callback may destroy
2426
+ * any gobj of the list, the current one included, or move one with
2427
+ * gobj_change_parent(). One it destroyed is stepped over, as the C walk
2428
+ * does; one it moved is no longer in this list, and is handed where the
2429
+ * walk finds it now (7.26.27 handed it here AND there); and a gobj is
2430
+ * never handed twice in one walk (`seen`), wherever it was moved.
2431
+ ***************************************************************/
2432
+ function walk_steps_over(child, owner, seen) {
2433
+ return gobj_is_destroying(child) || child.parent !== owner || seen.has(child);
2434
+ }
2435
+ function rc_walk_by_list(owner, seen, walk_type, cb_walking, user_data, user_data2) {
2436
+ let iter = [...owner.dl_children];
2393
2437
  let child;
2394
2438
  if (walk_type & walk_type_t.WALK_LAST2FIRST) for (let i = iter.length; i > 0; i--) {
2395
2439
  child = iter[i - 1];
2396
- if (gobj_is_destroying(child)) continue;
2440
+ if (walk_steps_over(child, owner, seen)) continue;
2441
+ seen.add(child);
2397
2442
  let ret = walk_ret(cb_walking(child, user_data, user_data2));
2398
2443
  if (ret < 0) return ret;
2399
2444
  }
2400
2445
  else for (let i = 0; i < iter.length; i++) {
2401
2446
  child = iter[i];
2402
- if (gobj_is_destroying(child)) continue;
2447
+ if (walk_steps_over(child, owner, seen)) continue;
2448
+ seen.add(child);
2403
2449
  let ret = walk_ret(cb_walking(child, user_data, user_data2));
2404
2450
  if (ret < 0) return ret;
2405
2451
  }
@@ -2414,24 +2460,23 @@ var gobjJs = (function(exports) {
2414
2460
  * or
2415
2461
  * WALK_LAST2FIRST
2416
2462
  ***************************************************************/
2417
- function rc_walk_by_level(iter_, walk_type, cb_walking, user_data, user_data2) {
2418
- let iter = [...iter_];
2419
- let ret = rc_walk_by_list(iter, walk_type, cb_walking, user_data, user_data2);
2420
- if (ret < 0) return ret;
2421
- let child;
2422
- if (walk_type & walk_type_t.WALK_LAST2FIRST) for (let i = iter.length; i > 0; i--) {
2423
- child = iter[i - 1];
2424
- if (gobj_is_destroying(child)) continue;
2425
- ret = rc_walk_by_level(child.dl_children, walk_type, cb_walking, user_data, user_data2);
2463
+ function rc_walk_by_level(owner, seen, walk_type, cb_walking, user_data, user_data2) {
2464
+ let iter = [...owner.dl_children];
2465
+ if (walk_type & walk_type_t.WALK_LAST2FIRST) iter.reverse();
2466
+ let handed = [];
2467
+ for (const child of iter) {
2468
+ if (walk_steps_over(child, owner, seen)) continue;
2469
+ seen.add(child);
2470
+ handed.push(child);
2471
+ let ret = walk_ret(cb_walking(child, user_data, user_data2));
2426
2472
  if (ret < 0) return ret;
2427
2473
  }
2428
- else for (let i = 0; i < iter.length; i++) {
2429
- child = iter[i];
2474
+ for (const child of handed) {
2430
2475
  if (gobj_is_destroying(child)) continue;
2431
- ret = rc_walk_by_level(child.dl_children, walk_type, cb_walking, user_data, user_data2);
2476
+ let ret = rc_walk_by_level(child, seen, walk_type, cb_walking, user_data, user_data2);
2432
2477
  if (ret < 0) return ret;
2433
2478
  }
2434
- return ret;
2479
+ return 0;
2435
2480
  }
2436
2481
  /***************************************************************
2437
2482
  * Walking
@@ -2448,27 +2493,29 @@ var gobjJs = (function(exports) {
2448
2493
  * or
2449
2494
  * WALK_LAST2FIRST
2450
2495
  ***************************************************************/
2451
- function rc_walk_by_tree(iter_, walk_type, cb_walking, user_data, user_data2) {
2452
- if (walk_type & walk_type_t.WALK_BYLEVEL) return rc_walk_by_level(iter_, walk_type, cb_walking, user_data, user_data2);
2453
- let iter = [...iter_];
2496
+ function rc_walk_by_tree(owner, seen, walk_type, cb_walking, user_data, user_data2) {
2497
+ if (walk_type & walk_type_t.WALK_BYLEVEL) return rc_walk_by_level(owner, seen, walk_type, cb_walking, user_data, user_data2);
2498
+ let iter = [...owner.dl_children];
2454
2499
  let ret;
2455
2500
  let child;
2456
2501
  if (walk_type & walk_type_t.WALK_BOTTOM2TOP) for (let i = iter.length; i > 0; i--) {
2457
2502
  child = iter[i - 1];
2458
- if (gobj_is_destroying(child)) continue;
2459
- ret = rc_walk_by_tree(child.dl_children, walk_type, cb_walking, user_data, user_data2);
2503
+ if (walk_steps_over(child, owner, seen)) continue;
2504
+ ret = rc_walk_by_tree(child, seen, walk_type, cb_walking, user_data, user_data2);
2460
2505
  if (ret < 0) return ret;
2461
2506
  if (gobj_is_destroying(child)) continue;
2507
+ seen.add(child);
2462
2508
  ret = walk_ret(cb_walking(child, user_data, user_data2));
2463
2509
  if (ret < 0) return ret;
2464
2510
  }
2465
2511
  else for (let i = 0; i < iter.length; i++) {
2466
2512
  child = iter[i];
2467
- if (gobj_is_destroying(child)) continue;
2513
+ if (walk_steps_over(child, owner, seen)) continue;
2514
+ seen.add(child);
2468
2515
  ret = walk_ret(cb_walking(child, user_data, user_data2));
2469
2516
  if (ret < 0) return ret;
2470
2517
  else if (ret === 0 && !gobj_is_destroying(child)) {
2471
- ret = rc_walk_by_tree(child.dl_children, walk_type, cb_walking, user_data, user_data2);
2518
+ ret = rc_walk_by_tree(child, seen, walk_type, cb_walking, user_data, user_data2);
2472
2519
  if (ret < 0) return ret;
2473
2520
  }
2474
2521
  }
@@ -2931,12 +2978,20 @@ var gobjJs = (function(exports) {
2931
2978
  function gobj_change_parent(gobj, parent) {
2932
2979
  if (gobj_is_destroying(gobj)) {
2933
2980
  log_error("gobj NULL or DESTROYED");
2934
- return null;
2981
+ return -1;
2935
2982
  }
2936
2983
  if (!(parent instanceof GObj)) {
2937
2984
  log_error(`${gobj_short_name(gobj)}: gobj_change_parent() BAD TYPE of parent`);
2938
2985
  return -1;
2939
2986
  }
2987
+ if (gobj_is_destroying(parent)) {
2988
+ log_error(`${gobj_short_name(gobj)}: gobj_change_parent() parent NULL or DESTROYED`);
2989
+ return -1;
2990
+ }
2991
+ for (let p = parent; p; p = p.parent) if (p === gobj) {
2992
+ log_error(`${gobj_short_name(gobj)}: gobj_change_parent() a gobj cannot be its own ancestor: ${gobj_short_name(parent)}`);
2993
+ return -1;
2994
+ }
2940
2995
  if (gobj.parent && gobj.parent.gclass.gmt.mt_child_removed) gobj.parent.gclass.gmt.mt_child_removed(gobj.parent, gobj);
2941
2996
  if (gobj.parent) {
2942
2997
  _remove_child(gobj.parent, gobj);
@@ -6101,7 +6156,10 @@ var gobjJs = (function(exports) {
6101
6156
  * for ever; a new corrupt entry is a new WARNING. Its text is never
6102
6157
  * shown (JSON.parse() quotes it in its error, and an entry can hold a
6103
6158
  * token). A store that cannot be read (blocked) is warned once per
6104
- * key, and the default is NOT written there. Outside a browser: the
6159
+ * key, and the default is NOT written there; one that cannot be
6160
+ * written (full) is warned once per key too: with `create` it was an
6161
+ * ERROR on every read, the default tried again each time. A key is
6162
+ * warned again after a read that goes well. Outside a browser: the
6105
6163
  * default.
6106
6164
  ********************************************/
6107
6165
  function kw_get_local_storage_value(key, default_value, create = false) {
@@ -6115,6 +6173,7 @@ var gobjJs = (function(exports) {
6115
6173
  warn_local_storage_once(key, `${where} cannot be read (storage blocked?): the default is used`);
6116
6174
  return default_value;
6117
6175
  }
6176
+ let went_well = true;
6118
6177
  let value = null;
6119
6178
  if (text !== null) try {
6120
6179
  value = JSON.parse(text);
@@ -6123,17 +6182,25 @@ var gobjJs = (function(exports) {
6123
6182
  try {
6124
6183
  store.removeItem(key);
6125
6184
  log_warning(`${where} does not hold json: removed, the default is used`);
6126
- local_storage_warned.delete(key);
6127
6185
  } catch (e2) {
6186
+ went_well = false;
6128
6187
  warn_local_storage_once(key, `${where} does not hold json and cannot be removed: the default is used`);
6129
6188
  }
6130
- text = void 0;
6131
6189
  }
6132
- if (text !== void 0) local_storage_warned.delete(key);
6133
6190
  if (value === null) {
6134
- if (create) kw_set_local_storage_value(key, default_value);
6135
- return default_value;
6191
+ value = default_value;
6192
+ if (create) {
6193
+ const default_text = json_text_of(default_value);
6194
+ if (default_text === void 0) log_error(`${where}: cannot write the default: a value with no json text`);
6195
+ else try {
6196
+ store.setItem(key, default_text);
6197
+ } catch (e) {
6198
+ went_well = false;
6199
+ warn_local_storage_once(key, `${where} cannot be written (storage full or blocked?): the default is used, not kept`);
6200
+ }
6201
+ }
6136
6202
  }
6203
+ if (went_well) local_storage_warned.delete(key);
6137
6204
  return value;
6138
6205
  }
6139
6206
  /********************************************
@@ -7373,29 +7440,68 @@ var gobjJs = (function(exports) {
7373
7440
  return -1;
7374
7441
  }
7375
7442
  /************************************************************
7443
+ * A new uuid v4 (RFC 9562).
7444
+ *
7445
+ * crypto.randomUUID() exists only in a secure context, so a page
7446
+ * served over http without TLS has none; crypto.getRandomValues()
7447
+ * exists there too. With no crypto at all, Math.random(): not fit
7448
+ * for a secret, and a uuid that names a node is not one.
7449
+ ************************************************************/
7450
+ function new_uuid() {
7451
+ const c = typeof window !== "undefined" && window.crypto || globalThis.crypto;
7452
+ if (c && typeof c.randomUUID === "function") return c.randomUUID();
7453
+ const b = /* @__PURE__ */ new Uint8Array(16);
7454
+ if (c && typeof c.getRandomValues === "function") c.getRandomValues(b);
7455
+ else for (let i = 0; i < b.length; i++) b[i] = Math.floor(Math.random() * 256);
7456
+ b[6] = b[6] & 15 | 64;
7457
+ b[8] = b[8] & 63 | 128;
7458
+ const h = Array.from(b, (x) => x.toString(16).padStart(2, "0")).join("");
7459
+ return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
7460
+ }
7461
+ /************************************************************
7462
+ * The uuid of this node: ONE for the life of the page.
7376
7463
  *
7464
+ * The one the store keeps ("host_uuid"), made and written there when
7465
+ * there is none. When the store cannot keep it (blocked, full, no
7466
+ * localStorage, outside a browser) the one this page made, warned
7467
+ * once. It goes in the node_uuid attribute of the yuno, the id of the
7468
+ * identity card and the `host` of every message the client sends, so
7469
+ * the first answer is kept and answered again: 7.26.27 made a new
7470
+ * uuid on every call when the store could not keep it, and answered
7471
+ * null without crypto.randomUUID(). A stored value that is not a
7472
+ * uuid (a foreign or corrupt write) is replaced, with a WARNING.
7377
7473
  ************************************************************/
7474
+ var uuid_of_this_page = null;
7475
+ var UUID_SHAPE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
7378
7476
  function node_uuid() {
7379
- let uuid = "";
7477
+ if (uuid_of_this_page) return uuid_of_this_page;
7380
7478
  const store = local_store();
7381
- if (store) {
7479
+ if (!store) {
7480
+ uuid_of_this_page = new_uuid();
7481
+ return uuid_of_this_page;
7482
+ }
7483
+ let uuid = null;
7484
+ try {
7485
+ uuid = store.getItem("host_uuid");
7486
+ } catch (e) {
7487
+ warn_local_storage_once("host_uuid", "node_uuid(): localStorage cannot be read (storage blocked?): a uuid of this page only");
7488
+ uuid_of_this_page = new_uuid();
7489
+ return uuid_of_this_page;
7490
+ }
7491
+ if (uuid && !UUID_SHAPE.test(uuid)) {
7492
+ log_warning("node_uuid(): the stored host_uuid is not a uuid: a new one replaces it");
7493
+ uuid = null;
7494
+ }
7495
+ if (!uuid) {
7496
+ uuid = new_uuid();
7382
7497
  try {
7383
- uuid = store.getItem("host_uuid");
7498
+ store.setItem("host_uuid", uuid);
7384
7499
  } catch (e) {
7385
- uuid = null;
7386
- warn_local_storage_once("host_uuid", "node_uuid(): localStorage cannot be read (storage blocked?): a uuid of this page only");
7387
- }
7388
- if (!uuid && window.crypto?.randomUUID) {
7389
- uuid = window.crypto.randomUUID();
7390
- try {
7391
- store.setItem("host_uuid", uuid);
7392
- } catch (e) {
7393
- warn_local_storage_once("host_uuid", "node_uuid(): localStorage cannot be written (storage full or blocked?): a uuid of this page only");
7394
- }
7500
+ warn_local_storage_once("host_uuid", "node_uuid(): localStorage cannot be written (storage full or blocked?): a uuid of this page only");
7395
7501
  }
7396
- } else if (typeof globalThis !== "undefined" && globalThis.crypto?.randomUUID) uuid = globalThis.crypto.randomUUID();
7397
- else uuid = "00000000-0000-0000-0000-000000000000";
7398
- return uuid;
7502
+ }
7503
+ uuid_of_this_page = uuid;
7504
+ return uuid_of_this_page;
7399
7505
  }
7400
7506
  /************************************************************
7401
7507
  * Build a cleaned name
@@ -8680,7 +8786,7 @@ var gobjJs = (function(exports) {
8680
8786
  SDATA(data_type_t.DTP_STRING, "cert_pem", sdata_flag_t.SDF_PERSIST, "", "SSL server certification, PEM str format"),
8681
8787
  SDATA(data_type_t.DTP_JSON, "extra_info", sdata_flag_t.SDF_RD, "{}", "dict data set by user, added to the identity card msg."),
8682
8788
  SDATA(data_type_t.DTP_LIST, "required_services", sdata_flag_t.SDF_RD, "[]", "Services this LINK requires (identity card). Empty = the yuno's `required_services`, as before. A yuno with several links to DIFFERENT backends must set it per link: the yuno-wide list is the union, so every backend is told the service names of all the others."),
8683
- SDATA(data_type_t.DTP_INTEGER, "timeout_retry", sdata_flag_t.SDF_RD, "5000", "First reconnect delay (ms), at least 1000. It BACKS OFF from here, doubling up to timeout_retry_max, and resets when a session is reached"),
8789
+ SDATA(data_type_t.DTP_INTEGER, "timeout_retry", sdata_flag_t.SDF_RD, "5000", "Base of the first reconnect delay (ms, ±20% jitter), at least 1000. It BACKS OFF from here, doubling up to timeout_retry_max, and resets when a session is reached"),
8684
8790
  SDATA(data_type_t.DTP_INTEGER, "timeout_retry_max", sdata_flag_t.SDF_RD, "60000", "Cap of the reconnect backoff (ms). Set it equal to timeout_retry for the old fixed-interval behaviour"),
8685
8791
  SDATA(data_type_t.DTP_INTEGER, "timeout_idack", sdata_flag_t.SDF_RD, "5000", "timeout waiting idAck"),
8686
8792
  SDATA(data_type_t.DTP_POINTER, "subscriber", 0, 0, "subscriber of output-events. If null then subscriber is the parent"),