@node-red/util 2.2.0 → 2.2.3

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 +32 -16
  3. package/package.json +3 -3
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}
@@ -630,7 +641,12 @@ function evaluateNodeProperty(value, type, node, msg, callback) {
630
641
  result = Date.now();
631
642
  } else if (type === 'bin') {
632
643
  var data = JSON.parse(value);
633
- result = Buffer.from(data);
644
+ if (Array.isArray(data) || (typeof(data) === "string")) {
645
+ result = Buffer.from(data);
646
+ }
647
+ else {
648
+ throw createError("INVALID_BUFFER_DATA", "Not string or array");
649
+ }
634
650
  } else if (type === 'msg' && msg) {
635
651
  try {
636
652
  result = getMessageProperty(msg,value);
@@ -785,7 +801,7 @@ function normaliseNodeTypeName(name) {
785
801
  function encodeObject(msg,opts) {
786
802
  try {
787
803
  var debuglength = 1000;
788
- if (opts && opts.hasOwnProperty('maxLength')) {
804
+ if (opts && hasOwnProperty.call(opts, 'maxLength')) {
789
805
  debuglength = opts.maxLength;
790
806
  }
791
807
  var msgType = typeof msg.msg;
@@ -795,7 +811,7 @@ function encodeObject(msg,opts) {
795
811
  if (msg.msg.name) {
796
812
  errorMsg.name = msg.msg.name;
797
813
  }
798
- if (msg.msg.hasOwnProperty('message')) {
814
+ if (hasOwnProperty.call(msg.msg, 'message')) {
799
815
  errorMsg.message = msg.msg.message;
800
816
  } else {
801
817
  errorMsg.message = msg.msg.toString();
@@ -809,7 +825,7 @@ function encodeObject(msg,opts) {
809
825
  }
810
826
  } else if (msg.msg && msgType === 'object') {
811
827
  try {
812
- msg.format = msg.msg.constructor.name || "Object";
828
+ msg.format = constructorName(msg.msg) || "Object";
813
829
  // Handle special case of msg.req/res objects from HTTP In node
814
830
  if (msg.format === "IncomingMessage" || msg.format === "ServerResponse") {
815
831
  msg.format = "Object";
@@ -836,7 +852,7 @@ function encodeObject(msg,opts) {
836
852
  length: msg.msg.length
837
853
  }
838
854
  }
839
- } else if (msg.msg && msg.msg.constructor.name === "Set") {
855
+ } else if (constructorName(msg.msg) === "Set") {
840
856
  msg.format = "set["+msg.msg.size+"]";
841
857
  msg.msg = {
842
858
  __enc__: true,
@@ -845,7 +861,7 @@ function encodeObject(msg,opts) {
845
861
  length: msg.msg.size
846
862
  }
847
863
  needsStringify = true;
848
- } else if (msg.msg && msg.msg.constructor.name === "Map") {
864
+ } else if (constructorName(msg.msg) === "Map") {
849
865
  msg.format = "map";
850
866
  msg.msg = {
851
867
  __enc__: true,
@@ -854,7 +870,7 @@ function encodeObject(msg,opts) {
854
870
  length: msg.msg.size
855
871
  }
856
872
  needsStringify = true;
857
- } else if (msg.msg && msg.msg.constructor.name === "RegExp") {
873
+ } else if (constructorName(msg.msg) === "RegExp") {
858
874
  msg.format = 'regexp';
859
875
  msg.msg = msg.msg.toString();
860
876
  }
@@ -904,25 +920,25 @@ function encodeObject(msg,opts) {
904
920
  if (value.length > debuglength) {
905
921
  value.data = value.data.slice(0,debuglength);
906
922
  }
907
- } else if (value.constructor.name === "ServerResponse") {
923
+ } else if (constructorName(value) === "ServerResponse") {
908
924
  value = "[internal]"
909
- } else if (value.constructor.name === "Socket") {
925
+ } else if (constructorName(value) === "Socket") {
910
926
  value = "[internal]"
911
- } else if (value.constructor.name === "Set") {
927
+ } else if (constructorName(value) === "Set") {
912
928
  value = {
913
929
  __enc__: true,
914
930
  type: "set",
915
931
  data: Array.from(value).slice(0,debuglength),
916
932
  length: value.size
917
933
  }
918
- } else if (value.constructor.name === "Map") {
934
+ } else if (constructorName(value) === "Map") {
919
935
  value = {
920
936
  __enc__: true,
921
937
  type: "map",
922
938
  data: Object.fromEntries(Array.from(value.entries()).slice(0,debuglength)),
923
939
  length: value.size
924
940
  }
925
- } else if (value.constructor.name === "RegExp") {
941
+ } else if (constructorName(value) === "RegExp") {
926
942
  value = {
927
943
  __enc__: true,
928
944
  type: "regexp",
@@ -974,7 +990,7 @@ function encodeObject(msg,opts) {
974
990
  if (e.name) {
975
991
  errorMsg.name = e.name;
976
992
  }
977
- if (e.hasOwnProperty('message')) {
993
+ if (hasOwnProperty.call(e, 'message')) {
978
994
  errorMsg.message = 'encodeObject Error: ['+e.message + '] Value: '+util.inspect(msg.msg);
979
995
  } else {
980
996
  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": "2.2.3",
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.10",
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
  }