@node-red/util 2.1.5 → 2.2.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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright JS Foundation and other contributors, http://js.foundation
1
+ Copyright OpenJS Foundation and other contributors, https://openjsf.org/
2
2
 
3
3
  Apache License
4
4
  Version 2.0, January 2004
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
@@ -522,6 +533,17 @@ function setObjectProperty(msg,prop,value,createMissing) {
522
533
  * @return {String} value of env var
523
534
  */
524
535
  function getSetting(node, name, flow_) {
536
+ if (node) {
537
+ if (name === "NR_NODE_NAME") {
538
+ return node.name;
539
+ }
540
+ if (name === "NR_NODE_ID") {
541
+ return node.id;
542
+ }
543
+ if (name === "NR_NODE_PATH") {
544
+ return node._path;
545
+ }
546
+ }
525
547
  var flow = (flow_ ? flow_ : (node ? node._flow : null));
526
548
  if (flow) {
527
549
  if (node && node.g) {
@@ -550,7 +572,7 @@ function getSetting(node, name, flow_) {
550
572
  * @memberof @node-red/util_util
551
573
  */
552
574
  function evaluateEnvProperty(value, node) {
553
- var flow = (node && node.hasOwnProperty("_flow")) ? node._flow : null;
575
+ var flow = (node && hasOwnProperty.call(node, "_flow")) ? node._flow : null;
554
576
  var result;
555
577
  if (/^\${[^}]+}$/.test(value)) {
556
578
  // ${ENV_VAR}
@@ -774,7 +796,7 @@ function normaliseNodeTypeName(name) {
774
796
  function encodeObject(msg,opts) {
775
797
  try {
776
798
  var debuglength = 1000;
777
- if (opts && opts.hasOwnProperty('maxLength')) {
799
+ if (opts && hasOwnProperty.call(opts, 'maxLength')) {
778
800
  debuglength = opts.maxLength;
779
801
  }
780
802
  var msgType = typeof msg.msg;
@@ -784,7 +806,7 @@ function encodeObject(msg,opts) {
784
806
  if (msg.msg.name) {
785
807
  errorMsg.name = msg.msg.name;
786
808
  }
787
- if (msg.msg.hasOwnProperty('message')) {
809
+ if (hasOwnProperty.call(msg.msg, 'message')) {
788
810
  errorMsg.message = msg.msg.message;
789
811
  } else {
790
812
  errorMsg.message = msg.msg.toString();
@@ -798,7 +820,7 @@ function encodeObject(msg,opts) {
798
820
  }
799
821
  } else if (msg.msg && msgType === 'object') {
800
822
  try {
801
- msg.format = msg.msg.constructor.name || "Object";
823
+ msg.format = constructorName(msg.msg) || "Object";
802
824
  // Handle special case of msg.req/res objects from HTTP In node
803
825
  if (msg.format === "IncomingMessage" || msg.format === "ServerResponse") {
804
826
  msg.format = "Object";
@@ -825,7 +847,7 @@ function encodeObject(msg,opts) {
825
847
  length: msg.msg.length
826
848
  }
827
849
  }
828
- } else if (msg.msg && msg.msg.constructor.name === "Set") {
850
+ } else if (constructorName(msg.msg) === "Set") {
829
851
  msg.format = "set["+msg.msg.size+"]";
830
852
  msg.msg = {
831
853
  __enc__: true,
@@ -834,7 +856,7 @@ function encodeObject(msg,opts) {
834
856
  length: msg.msg.size
835
857
  }
836
858
  needsStringify = true;
837
- } else if (msg.msg && msg.msg.constructor.name === "Map") {
859
+ } else if (constructorName(msg.msg) === "Map") {
838
860
  msg.format = "map";
839
861
  msg.msg = {
840
862
  __enc__: true,
@@ -843,7 +865,7 @@ function encodeObject(msg,opts) {
843
865
  length: msg.msg.size
844
866
  }
845
867
  needsStringify = true;
846
- } else if (msg.msg && msg.msg.constructor.name === "RegExp") {
868
+ } else if (constructorName(msg.msg) === "RegExp") {
847
869
  msg.format = 'regexp';
848
870
  msg.msg = msg.msg.toString();
849
871
  }
@@ -893,25 +915,25 @@ function encodeObject(msg,opts) {
893
915
  if (value.length > debuglength) {
894
916
  value.data = value.data.slice(0,debuglength);
895
917
  }
896
- } else if (value.constructor.name === "ServerResponse") {
918
+ } else if (constructorName(value) === "ServerResponse") {
897
919
  value = "[internal]"
898
- } else if (value.constructor.name === "Socket") {
920
+ } else if (constructorName(value) === "Socket") {
899
921
  value = "[internal]"
900
- } else if (value.constructor.name === "Set") {
922
+ } else if (constructorName(value) === "Set") {
901
923
  value = {
902
924
  __enc__: true,
903
925
  type: "set",
904
926
  data: Array.from(value).slice(0,debuglength),
905
927
  length: value.size
906
928
  }
907
- } else if (value.constructor.name === "Map") {
929
+ } else if (constructorName(value) === "Map") {
908
930
  value = {
909
931
  __enc__: true,
910
932
  type: "map",
911
933
  data: Object.fromEntries(Array.from(value.entries()).slice(0,debuglength)),
912
934
  length: value.size
913
935
  }
914
- } else if (value.constructor.name === "RegExp") {
936
+ } else if (constructorName(value) === "RegExp") {
915
937
  value = {
916
938
  __enc__: true,
917
939
  type: "regexp",
@@ -963,7 +985,7 @@ function encodeObject(msg,opts) {
963
985
  if (e.name) {
964
986
  errorMsg.name = e.name;
965
987
  }
966
- if (e.hasOwnProperty('message')) {
988
+ if (hasOwnProperty.call(e, 'message')) {
967
989
  errorMsg.message = 'encodeObject Error: ['+e.message + '] Value: '+util.inspect(msg.msg);
968
990
  } else {
969
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.1.5",
3
+ "version": "2.2.1",
4
4
  "license": "Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,9 +16,9 @@
16
16
  ],
17
17
  "dependencies": {
18
18
  "fs-extra": "10.0.0",
19
- "i18next": "21.6.6",
19
+ "i18next": "21.6.11",
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
23
  "moment-timezone": "0.5.34"
24
24
  }