@node-red/util 2.2.0 → 3.0.0-beta.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.
Files changed (3) hide show
  1. package/lib/hooks.js +18 -1
  2. package/lib/util.js +26 -15
  3. package/package.json +5 -4
package/lib/hooks.js CHANGED
@@ -67,8 +67,25 @@ function add(hookId, callback) {
67
67
  throw new Error("Hook "+hookId+" already registered")
68
68
  }
69
69
  // Get location of calling code
70
+ let callModule;
70
71
  const stack = new Error().stack;
71
- const callModule = stack.split("\n")[2].split("(")[1].slice(0,-1);
72
+ const stackEntries = stack.split("\n").slice(1);//drop 1st line (error message)
73
+ const stackEntry2 = stackEntries[1];//get 2nd stack entry
74
+ if (stackEntry2) {
75
+ try {
76
+ if (stackEntry2.indexOf(" (") >= 0) {
77
+ callModule = stackEntry2.split("(")[1].slice(0, -1);
78
+ } else {
79
+ callModule = stackEntry2.split(" ").slice(-1)[0];
80
+ }
81
+ } catch (error) {
82
+ Log.debug(`Unable to determined module when adding hook '${hookId}'. Stack:\n${stackEntries.join("\n")}`);
83
+ callModule = "unknown:0:0";
84
+ }
85
+ } else {
86
+ Log.debug(`Unable to determined module when adding hook '${hookId}'. Stack:\n${stackEntries.join("\n")}`);
87
+ callModule = "unknown:0:0";
88
+ }
72
89
  Log.debug(`Adding hook '${hookId}' from ${callModule}`);
73
90
 
74
91
  const hookItem = {cb:callback, location: callModule, previousHook: null, nextHook: null }
package/lib/util.js CHANGED
@@ -24,6 +24,17 @@ const jsonata = require("jsonata");
24
24
  const moment = require("moment-timezone");
25
25
  const safeJSONStringify = require("json-stringify-safe");
26
26
  const util = require("util");
27
+ const { hasOwnProperty } = Object.prototype;
28
+
29
+ /**
30
+ * Safely returns the object construtor name.
31
+ * @return {String} the name of the object constructor if it exists, empty string otherwise.
32
+ */
33
+ function constructorName(obj) {
34
+ // Note: This function could be replaced by optional chaining in Node.js 14+:
35
+ // obj?.constructor?.name
36
+ return obj && obj.constructor ? obj.constructor.name : '';
37
+ }
27
38
 
28
39
  /**
29
40
  * Generates a psuedo-unique-random id.
@@ -171,7 +182,7 @@ function compareObjects(obj1,obj2) {
171
182
  }
172
183
  for (var k in obj1) {
173
184
  /* istanbul ignore else */
174
- if (obj1.hasOwnProperty(k)) {
185
+ if (hasOwnProperty.call(obj1, k)) {
175
186
  if (!compareObjects(obj1[k],obj2[k])) {
176
187
  return false;
177
188
  }
@@ -462,7 +473,7 @@ function setObjectProperty(msg,prop,value,createMissing) {
462
473
  for (var i=0;i<length-1;i++) {
463
474
  key = msgPropParts[i];
464
475
  if (typeof key === 'string' || (typeof key === 'number' && !Array.isArray(obj))) {
465
- if (obj.hasOwnProperty(key)) {
476
+ if (hasOwnProperty.call(obj, key)) {
466
477
  if (length > 1 && ((typeof obj[key] !== "object" && typeof obj[key] !== "function") || obj[key] === null)) {
467
478
  // Break out early as we cannot create a property beneath
468
479
  // this type of value
@@ -561,7 +572,7 @@ function getSetting(node, name, flow_) {
561
572
  * @memberof @node-red/util_util
562
573
  */
563
574
  function evaluateEnvProperty(value, node) {
564
- var flow = (node && node.hasOwnProperty("_flow")) ? node._flow : null;
575
+ var flow = (node && hasOwnProperty.call(node, "_flow")) ? node._flow : null;
565
576
  var result;
566
577
  if (/^\${[^}]+}$/.test(value)) {
567
578
  // ${ENV_VAR}
@@ -785,7 +796,7 @@ function normaliseNodeTypeName(name) {
785
796
  function encodeObject(msg,opts) {
786
797
  try {
787
798
  var debuglength = 1000;
788
- if (opts && opts.hasOwnProperty('maxLength')) {
799
+ if (opts && hasOwnProperty.call(opts, 'maxLength')) {
789
800
  debuglength = opts.maxLength;
790
801
  }
791
802
  var msgType = typeof msg.msg;
@@ -795,7 +806,7 @@ function encodeObject(msg,opts) {
795
806
  if (msg.msg.name) {
796
807
  errorMsg.name = msg.msg.name;
797
808
  }
798
- if (msg.msg.hasOwnProperty('message')) {
809
+ if (hasOwnProperty.call(msg.msg, 'message')) {
799
810
  errorMsg.message = msg.msg.message;
800
811
  } else {
801
812
  errorMsg.message = msg.msg.toString();
@@ -809,7 +820,7 @@ function encodeObject(msg,opts) {
809
820
  }
810
821
  } else if (msg.msg && msgType === 'object') {
811
822
  try {
812
- msg.format = msg.msg.constructor.name || "Object";
823
+ msg.format = constructorName(msg.msg) || "Object";
813
824
  // Handle special case of msg.req/res objects from HTTP In node
814
825
  if (msg.format === "IncomingMessage" || msg.format === "ServerResponse") {
815
826
  msg.format = "Object";
@@ -836,7 +847,7 @@ function encodeObject(msg,opts) {
836
847
  length: msg.msg.length
837
848
  }
838
849
  }
839
- } else if (msg.msg && msg.msg.constructor.name === "Set") {
850
+ } else if (constructorName(msg.msg) === "Set") {
840
851
  msg.format = "set["+msg.msg.size+"]";
841
852
  msg.msg = {
842
853
  __enc__: true,
@@ -845,7 +856,7 @@ function encodeObject(msg,opts) {
845
856
  length: msg.msg.size
846
857
  }
847
858
  needsStringify = true;
848
- } else if (msg.msg && msg.msg.constructor.name === "Map") {
859
+ } else if (constructorName(msg.msg) === "Map") {
849
860
  msg.format = "map";
850
861
  msg.msg = {
851
862
  __enc__: true,
@@ -854,7 +865,7 @@ function encodeObject(msg,opts) {
854
865
  length: msg.msg.size
855
866
  }
856
867
  needsStringify = true;
857
- } else if (msg.msg && msg.msg.constructor.name === "RegExp") {
868
+ } else if (constructorName(msg.msg) === "RegExp") {
858
869
  msg.format = 'regexp';
859
870
  msg.msg = msg.msg.toString();
860
871
  }
@@ -904,25 +915,25 @@ function encodeObject(msg,opts) {
904
915
  if (value.length > debuglength) {
905
916
  value.data = value.data.slice(0,debuglength);
906
917
  }
907
- } else if (value.constructor.name === "ServerResponse") {
918
+ } else if (constructorName(value) === "ServerResponse") {
908
919
  value = "[internal]"
909
- } else if (value.constructor.name === "Socket") {
920
+ } else if (constructorName(value) === "Socket") {
910
921
  value = "[internal]"
911
- } else if (value.constructor.name === "Set") {
922
+ } else if (constructorName(value) === "Set") {
912
923
  value = {
913
924
  __enc__: true,
914
925
  type: "set",
915
926
  data: Array.from(value).slice(0,debuglength),
916
927
  length: value.size
917
928
  }
918
- } else if (value.constructor.name === "Map") {
929
+ } else if (constructorName(value) === "Map") {
919
930
  value = {
920
931
  __enc__: true,
921
932
  type: "map",
922
933
  data: Object.fromEntries(Array.from(value.entries()).slice(0,debuglength)),
923
934
  length: value.size
924
935
  }
925
- } else if (value.constructor.name === "RegExp") {
936
+ } else if (constructorName(value) === "RegExp") {
926
937
  value = {
927
938
  __enc__: true,
928
939
  type: "regexp",
@@ -974,7 +985,7 @@ function encodeObject(msg,opts) {
974
985
  if (e.name) {
975
986
  errorMsg.name = e.name;
976
987
  }
977
- if (e.hasOwnProperty('message')) {
988
+ if (hasOwnProperty.call(e, 'message')) {
978
989
  errorMsg.message = 'encodeObject Error: ['+e.message + '] Value: '+util.inspect(msg.msg);
979
990
  } else {
980
991
  errorMsg.message = 'encodeObject Error: ['+e.toString() + '] Value: '+util.inspect(msg.msg);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node-red/util",
3
- "version": "2.2.0",
3
+ "version": "3.0.0-beta.1",
4
4
  "license": "Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,11 +15,12 @@
15
15
  }
16
16
  ],
17
17
  "dependencies": {
18
- "fs-extra": "10.0.0",
19
- "i18next": "21.6.10",
18
+ "fs-extra": "10.1.0",
19
+ "i18next": "21.6.16",
20
20
  "json-stringify-safe": "5.0.1",
21
- "jsonata": "1.8.5",
21
+ "jsonata": "1.8.6",
22
22
  "lodash.clonedeep": "^4.5.0",
23
+ "moment": "2.29.3",
23
24
  "moment-timezone": "0.5.34"
24
25
  }
25
26
  }