@yuneta/gobj-js 7.26.0 → 7.26.1

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.
package/README.md CHANGED
@@ -610,9 +610,10 @@ kw_clone_by_not_keys(gobj, kw, keys, verbose)
610
610
  // A filter value is compared as a value: {online: false}, {channel: 0}
611
611
  // and {name: ""} match (up to 7.25.9 a false value never did):
612
612
  // kw_select(null, [{id: "a", online: false}], {online: false}) → [{id: "a", ...}]
613
- // A key that names the prototype ("__proto__", "constructor",
614
- // "prototype") is a key like any other, as in C: it is read only when the
615
- // kw holds it, and written as the kw's own key, never into Object.prototype.
613
+ // A key is read only when the kw OWNS it ("toString" or "constructor" on
614
+ // a plain {} are not there) and written as its own key, "__proto__"
615
+ // included, as in C: never into a prototype. The helpers that copy
616
+ // (json_object_update(), kw_clone_by_*(), flat_apply()...) do the same.
616
617
 
617
618
  // Flags: KW_REQUIRED, KW_CREATE, KW_EXTRACT, KW_RECURSIVE, KW_WILD_NUMBER
618
619
 
@@ -895,10 +895,13 @@ function json2item(gobj, sdata, it, jn_value_) {
895
895
  break;
896
896
  case data_type_t.DTP_JSON:
897
897
  jn_value2 = jn_value_;
898
- if (is_string(jn_value_)) try {
899
- jn_value2 = JSON.parse(jn_value_);
900
- } catch (error) {
901
- jn_value2 = jn_value_;
898
+ if (is_string(jn_value_)) {
899
+ const t = jn_value_.trim();
900
+ if (t.startsWith("{") || t.startsWith("[")) try {
901
+ jn_value2 = JSON.parse(t);
902
+ } catch (error) {
903
+ jn_value2 = jn_value_;
904
+ }
902
905
  }
903
906
  break;
904
907
  case data_type_t.DTP_POINTER: jn_value2 = jn_value_;
@@ -2599,7 +2602,7 @@ function gobj_send_event(dst, event, kw, src) {
2599
2602
  } catch (e) {
2600
2603
  __inside__--;
2601
2604
  if (!(e && e.__gobj_logged__)) {
2602
- log_error(`${gobj_short_name(dst)}: action of ${event} in ${dst.current_state.state_name} THREW: ${e && e.stack ? e.stack : e}`);
2605
+ log_error(`${gobj_short_name(dst)}: action of ${event} in ${state.state_name} THREW: ${e && e.stack ? e.stack : e}`);
2603
2606
  try {
2604
2607
  e.__gobj_logged__ = true;
2605
2608
  } catch (e2) {}
@@ -3607,7 +3610,7 @@ function _json_mask_secrets(jn, memo, depth, budget) {
3607
3610
  if ((is_secret_name(key.slice(-128)) || value_is_secret && key === "value") && value !== void 0 && value !== null && value !== "") shown = "********";
3608
3611
  else shown = _json_mask_secrets(value, memo, depth + 1, budget);
3609
3612
  if (shown !== value) changed = true;
3610
- result[key] = shown;
3613
+ set_own_key(result, key, shown);
3611
3614
  }
3612
3615
  if (truncated) result["<more>"] = MASK_TOO_LARGE;
3613
3616
  }
@@ -3679,7 +3682,7 @@ function json_is_identical(kw1, kw2) {
3679
3682
  ************************************************************/
3680
3683
  function json_object_update(destination, source) {
3681
3684
  if (!source) return destination;
3682
- for (let property of Object.keys(source)) destination[property] = source[property];
3685
+ for (let property of Object.keys(source)) set_own_key(destination, property, source[property]);
3683
3686
  return destination;
3684
3687
  }
3685
3688
  /************************************************************
@@ -3688,7 +3691,7 @@ function json_object_update(destination, source) {
3688
3691
  ************************************************************/
3689
3692
  function json_object_update_existing(destination, source) {
3690
3693
  if (!source) return destination;
3691
- for (let property in source) if (source.hasOwnProperty(property) && destination.hasOwnProperty(property)) destination[property] = source[property];
3694
+ for (let property in source) if (Object.prototype.hasOwnProperty.call(source, property) && Object.prototype.hasOwnProperty.call(destination, property)) set_own_key(destination, property, source[property]);
3692
3695
  return destination;
3693
3696
  }
3694
3697
  /************************************************************
@@ -3697,32 +3700,32 @@ function json_object_update_existing(destination, source) {
3697
3700
  ************************************************************/
3698
3701
  function json_object_update_missing(destination, source) {
3699
3702
  if (!source) return destination;
3700
- for (let property in source) if (source.hasOwnProperty(property) && !destination.hasOwnProperty(property)) destination[property] = source[property];
3703
+ for (let property in source) if (Object.prototype.hasOwnProperty.call(source, property) && !Object.prototype.hasOwnProperty.call(destination, property)) set_own_key(destination, property, source[property]);
3701
3704
  return destination;
3702
3705
  }
3703
3706
  /************************************************************
3704
3707
  * Simulate jansson function
3705
3708
  ************************************************************/
3706
3709
  function json_object_get(o, key) {
3707
- if (o && o.hasOwnProperty(key)) return o[key];
3710
+ if (o && Object.prototype.hasOwnProperty.call(o, key)) return o[key];
3708
3711
  }
3709
3712
  /************************************************************
3710
3713
  * Simulate jansson function
3711
3714
  ************************************************************/
3712
3715
  function json_object_del(o, k) {
3713
- if (o && o.hasOwnProperty(k)) delete o[k];
3716
+ if (o && Object.prototype.hasOwnProperty.call(o, k)) delete o[k];
3714
3717
  }
3715
3718
  /************************************************************
3716
3719
  * Simulate jansson function
3717
3720
  ************************************************************/
3718
3721
  function json_object_set(o, k, v) {
3719
- o[k] = v;
3722
+ set_own_key(o, k, v);
3720
3723
  }
3721
3724
  /************************************************************
3722
3725
  * Simulate jansson function
3723
3726
  ************************************************************/
3724
3727
  function json_object_set_new(o, k, v) {
3725
- o[k] = v;
3728
+ set_own_key(o, k, v);
3726
3729
  }
3727
3730
  /************************************************************
3728
3731
  * Simulate jansson function
@@ -3966,22 +3969,20 @@ function kw_pop(kw1, kw2) {
3966
3969
  ************************************************************/
3967
3970
  function kw_has_key(kw, key) {
3968
3971
  if (key in kw) {
3969
- if (kw.hasOwnProperty(key)) return true;
3972
+ if (Object.prototype.hasOwnProperty.call(kw, key)) return true;
3970
3973
  }
3971
3974
  return false;
3972
3975
  }
3973
3976
  /************************************************************
3974
3977
  * A key that reaches the prototype chain instead of the data:
3975
- * "__proto__", "constructor" or "prototype" when the object
3976
- * does not hold it as its OWN key. A walk through one of them
3978
+ * any key the object does not hold as its OWN key. A walk through one of them
3977
3979
  * read, or WROTE, Object.prototype: the flat id
3978
3980
  * "__proto__`polluted" set a key on every object of the page.
3979
3981
  * In C they are keys like any other, so here too: read them
3980
3982
  * only as own keys, and write "__proto__" as an own key.
3981
3983
  ************************************************************/
3982
3984
  function is_prototype_segment(obj, key) {
3983
- if (key === "__proto__" || key === "constructor" || key === "prototype") return !Object.prototype.hasOwnProperty.call(obj, key);
3984
- return false;
3985
+ return !Object.prototype.hasOwnProperty.call(obj, key);
3985
3986
  }
3986
3987
  function set_own_key(obj, key, value) {
3987
3988
  if (key === "__proto__") Object.defineProperty(obj, key, {
@@ -4564,14 +4565,14 @@ function kw_clone_by_keys(gobj, kw, keys, verbose) {
4564
4565
  let kw_clone = {};
4565
4566
  if (is_string(keys) && !empty_string(keys)) {
4566
4567
  let key = keys;
4567
- if (kw.hasOwnProperty(key)) kw_clone[key] = kw[key];
4568
+ if (Object.prototype.hasOwnProperty.call(kw, key)) set_own_key(kw_clone, key, kw[key]);
4568
4569
  else if (verbose) log_error(`${gobj_short_name(gobj)}: key not found: ${key}`);
4569
4570
  } else if (is_object(keys) && json_object_size(keys) > 0) {
4570
- for (let key of Object.keys(keys)) if (kw.hasOwnProperty(key)) kw_clone[key] = kw[key];
4571
+ for (let key of Object.keys(keys)) if (Object.prototype.hasOwnProperty.call(kw, key)) set_own_key(kw_clone, key, kw[key]);
4571
4572
  else if (verbose) log_error(`${gobj_short_name(gobj)}: key not found: ${key}`);
4572
4573
  } else if (is_array(keys) && json_array_size(keys) > 0) for (let j = 0; j < keys.length; j++) {
4573
4574
  let key = keys[j];
4574
- if (kw.hasOwnProperty(key)) kw_clone[key] = kw[key];
4575
+ if (Object.prototype.hasOwnProperty.call(kw, key)) set_own_key(kw_clone, key, kw[key]);
4575
4576
  else if (verbose) log_error(`${gobj_short_name(gobj)}: key not found: ${key}`);
4576
4577
  }
4577
4578
  else return kw;
@@ -4593,14 +4594,14 @@ function kw_clone_by_not_keys(gobj, kw, keys, verbose) {
4593
4594
  json_object_update(kw_clone, kw);
4594
4595
  if (is_string(keys) && !empty_string(keys)) {
4595
4596
  let key = keys;
4596
- if (kw.hasOwnProperty(key)) json_object_del(kw_clone, key);
4597
+ if (Object.prototype.hasOwnProperty.call(kw, key)) json_object_del(kw_clone, key);
4597
4598
  else if (verbose) log_error(`${gobj_short_name(gobj)}: key not found: ${key}`);
4598
4599
  } else if (is_object(keys) && json_object_size(keys) > 0) {
4599
- for (let key of Object.keys(keys)) if (kw.hasOwnProperty(key)) json_object_del(kw_clone, key);
4600
+ for (let key of Object.keys(keys)) if (Object.prototype.hasOwnProperty.call(kw, key)) json_object_del(kw_clone, key);
4600
4601
  else if (verbose) log_error(`${gobj_short_name(gobj)}: key not found: ${key}`);
4601
4602
  } else if (is_array(keys) && json_array_size(keys) > 0) for (let j = 0; j < keys.length; j++) {
4602
4603
  let key = keys[j];
4603
- if (kw.hasOwnProperty(key)) json_object_del(kw_clone, key);
4604
+ if (Object.prototype.hasOwnProperty.call(kw, key)) json_object_del(kw_clone, key);
4604
4605
  else if (verbose) log_error(`${gobj_short_name(gobj)}: key not found: ${key}`);
4605
4606
  }
4606
4607
  else return {};
@@ -4831,7 +4832,7 @@ function kwid_get_ids(gobj, ids) {
4831
4832
  let new_ids = [];
4832
4833
  if (is_string(ids)) new_ids.push(ids);
4833
4834
  else if (is_object(ids)) {
4834
- for (let id in ids) if (ids.hasOwnProperty(id)) new_ids.push(id);
4835
+ for (let id in ids) if (Object.prototype.hasOwnProperty.call(ids, id)) new_ids.push(id);
4835
4836
  } else if (is_array(ids)) ids.forEach(function(item) {
4836
4837
  if (is_string(item)) {
4837
4838
  if (!empty_string(item)) new_ids.push(item);
@@ -5084,7 +5085,7 @@ function send_http_json_post(url, data, on_response) {
5084
5085
  function _traverse_dict(obj, callback, full_path) {
5085
5086
  if (full_path === void 0 || !is_string(full_path)) full_path = "";
5086
5087
  for (let key of Object.keys(obj)) {
5087
- if (!obj.hasOwnProperty(key)) continue;
5088
+ if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
5088
5089
  let sufix = (full_path.length ? "`" : "") + key;
5089
5090
  full_path += sufix;
5090
5091
  callback.apply(this, [
@@ -5965,7 +5966,7 @@ function json2flat(jn) {
5965
5966
  return;
5966
5967
  }
5967
5968
  if (depth === 0) return;
5968
- flat[path] = value;
5969
+ set_own_key(flat, path, value);
5969
5970
  };
5970
5971
  if (jn === void 0 || jn === null) return flat;
5971
5972
  walk(jn, "", 0);
@@ -6067,11 +6068,11 @@ function flat_apply(flat, diff) {
6067
6068
  if (flat === null || typeof flat !== "object" || Array.isArray(flat)) throw new Error("flat_apply: a flat json is an object of id -> value");
6068
6069
  if (diff === null || typeof diff !== "object") throw new Error("flat_apply: a diff is the object flat_diff() answers");
6069
6070
  for (const key of Object.keys(diff.removed || {})) delete flat[key];
6070
- for (const key of Object.keys(diff.added || {})) flat[key] = diff.added[key];
6071
+ for (const key of Object.keys(diff.added || {})) set_own_key(flat, key, diff.added[key]);
6071
6072
  for (const key of Object.keys(diff.changed || {})) {
6072
6073
  const change = diff.changed[key];
6073
6074
  if (!change || !Object.prototype.hasOwnProperty.call(change, "to")) throw new Error(`flat_apply: changed '${key}' without a 'to'`);
6074
- flat[key] = change.to;
6075
+ set_own_key(flat, key, change.to);
6075
6076
  }
6076
6077
  return flat;
6077
6078
  }
@@ -7213,6 +7214,7 @@ function setup_websocket(gobj) {
7213
7214
  if (gobj.priv.websocket) log_error(`Websocket ALREADY setup`);
7214
7215
  const url = gobj_read_str_attr(gobj, "url");
7215
7216
  log_debug(`====> Starting WebSocket to '${url}' (${gobj_short_name(gobj)})`);
7217
+ const ws_seq = ++gobj.priv.ws_seq;
7216
7218
  let websocket;
7217
7219
  try {
7218
7220
  websocket = new WebSocket(url);
@@ -7224,7 +7226,6 @@ function setup_websocket(gobj) {
7224
7226
  log_error(`${gobj_short_name(gobj)}: Failed to initialize WebSocket.`);
7225
7227
  return null;
7226
7228
  }
7227
- const ws_seq = ++gobj.priv.ws_seq;
7228
7229
  websocket.onopen = () => {
7229
7230
  if (ws_seq !== gobj.priv.ws_seq || gobj_is_destroying(gobj)) return;
7230
7231
  gobj_send_event(gobj, "EV_ON_OPEN", {