node-red-contrib-unifi-ultimate 0.1.8 → 0.1.9

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.
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+
3
+ // Text-like media types that are safe to decode as utf8 text.
4
+ const TEXTUAL_EXACT_CONTENT_TYPES = [
5
+ "application/x-www-form-urlencoded",
6
+ "application/xml",
7
+ "application/javascript",
8
+ "application/ecmascript"
9
+ ];
10
+
11
+ function normalizeContentType(contentType) {
12
+ const value = Array.isArray(contentType) ? contentType[0] : contentType;
13
+ return typeof value === "string" ? value.toLowerCase().split(";")[0].trim() : "";
14
+ }
15
+
16
+ function maybeParseBody(contentType, buffer) {
17
+ // Remove optional charset/params and normalize case.
18
+ const normalizedType = normalizeContentType(contentType);
19
+
20
+ if (!buffer.length) {
21
+ return "";
22
+ }
23
+
24
+ // Accept standard JSON and vendor/media types like application/problem+json.
25
+ const isJsonType = normalizedType === "application/json" || normalizedType.endsWith("+json");
26
+ // Classify media types that are text-based and can be safely coerced to string.
27
+ const isTextType = normalizedType.startsWith("text/")
28
+ || isJsonType
29
+ || normalizedType.endsWith("+xml")
30
+ || TEXTUAL_EXACT_CONTENT_TYPES.includes(normalizedType);
31
+
32
+ // Prefer safety: if the response explicitly declares a non-text media type,
33
+ // keep it as Buffer to avoid lossy utf8 coercion.
34
+ if (normalizedType && !isTextType) {
35
+ return buffer;
36
+ }
37
+
38
+ const raw = buffer.toString("utf8");
39
+ if (!raw) {
40
+ return raw;
41
+ }
42
+
43
+ const trimmed = raw.trim();
44
+ // "Looks like JSON" probing is allowed only for unknown/textual responses.
45
+ const canProbeJsonFromShape = !normalizedType || normalizedType.startsWith("text/");
46
+ const looksJson = trimmed.startsWith("{") || trimmed.startsWith("[");
47
+
48
+ if (isJsonType || (canProbeJsonFromShape && looksJson)) {
49
+ try {
50
+ return JSON.parse(trimmed);
51
+ } catch (error) {
52
+ return raw;
53
+ }
54
+ }
55
+
56
+ return raw;
57
+ }
58
+
59
+ module.exports = {
60
+ maybeParseBody
61
+ };
@@ -2,6 +2,7 @@
2
2
 
3
3
  const http = require("http");
4
4
  const https = require("https");
5
+ const { maybeParseBody } = require("./http-response-utils");
5
6
 
6
7
  function normalizeHost(value) {
7
8
  // Access controllers usually live on port 12445. Accept bare hosts, host:port
@@ -68,29 +69,6 @@ function buildQueryString(query) {
68
69
  return rendered ? `?${rendered}` : "";
69
70
  }
70
71
 
71
- function maybeParseBody(contentType, buffer) {
72
- // Access can still return JSON with inconsistent headers, so detect it from
73
- // the payload shape as well.
74
- const raw = buffer.toString("utf8");
75
- if (!raw) {
76
- return raw;
77
- }
78
-
79
- const trimmed = raw.trim();
80
- const isJsonType = typeof contentType === "string" && contentType.includes("application/json");
81
- const looksJson = trimmed.startsWith("{") || trimmed.startsWith("[");
82
-
83
- if (isJsonType || looksJson) {
84
- try {
85
- return JSON.parse(trimmed);
86
- } catch (error) {
87
- return raw;
88
- }
89
- }
90
-
91
- return raw;
92
- }
93
-
94
72
  function doRequest(url, options, body) {
95
73
  return new Promise((resolve, reject) => {
96
74
  // Use the native transports to avoid extra runtime dependencies inside
@@ -2,6 +2,7 @@
2
2
 
3
3
  const http = require("http");
4
4
  const https = require("https");
5
+ const { maybeParseBody } = require("./http-response-utils");
5
6
 
6
7
  function normalizeHost(value) {
7
8
  // Accept either a bare host or a full URL from the editor and reduce it to
@@ -49,29 +50,6 @@ function buildQueryString(query) {
49
50
  return rendered ? `?${rendered}` : "";
50
51
  }
51
52
 
52
- function maybeParseBody(contentType, buffer) {
53
- // Some UniFi endpoints return JSON with a missing/odd content-type header,
54
- // so also inspect the raw payload before deciding to parse.
55
- const raw = buffer.toString("utf8");
56
- if (!raw) {
57
- return raw;
58
- }
59
-
60
- const trimmed = raw.trim();
61
- const isJsonType = typeof contentType === "string" && contentType.includes("application/json");
62
- const looksJson = trimmed.startsWith("{") || trimmed.startsWith("[");
63
-
64
- if (isJsonType || looksJson) {
65
- try {
66
- return JSON.parse(trimmed);
67
- } catch (error) {
68
- return raw;
69
- }
70
- }
71
-
72
- return raw;
73
- }
74
-
75
53
  function doRequest(url, options, body) {
76
54
  return new Promise((resolve, reject) => {
77
55
  // Stay on the built-in http/https clients to keep node dependencies
@@ -2,6 +2,7 @@
2
2
 
3
3
  const http = require("http");
4
4
  const https = require("https");
5
+ const { maybeParseBody } = require("./http-response-utils");
5
6
 
6
7
  function normalizeHost(value) {
7
8
  // Protect editor fields may contain a host or a full URL; strip everything
@@ -46,29 +47,6 @@ function buildQueryString(query) {
46
47
  return rendered ? `?${rendered}` : "";
47
48
  }
48
49
 
49
- function maybeParseBody(contentType, buffer) {
50
- // Protect sometimes returns JSON-like payloads even when headers are not
51
- // perfectly descriptive, so inspect both header and content shape.
52
- const raw = buffer.toString("utf8");
53
- if (!raw) {
54
- return raw;
55
- }
56
-
57
- const trimmed = raw.trim();
58
- const isJsonType = typeof contentType === "string" && contentType.includes("application/json");
59
- const looksJson = trimmed.startsWith("{") || trimmed.startsWith("[");
60
-
61
- if (isJsonType || looksJson) {
62
- try {
63
- return JSON.parse(trimmed);
64
- } catch (error) {
65
- return raw;
66
- }
67
- }
68
-
69
- return raw;
70
- }
71
-
72
50
  function doRequest(url, options, body) {
73
51
  return new Promise((resolve, reject) => {
74
52
  // Keep request plumbing dependency-free for easier Node-RED packaging.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-unifi-ultimate",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Node-RED nodes for Ubiquiti UniFi integrations, including UniFi Protect, UniFi Access, and UniFi Network.",
5
5
  "files": [
6
6
  "nodes/",
@@ -46,7 +46,7 @@
46
46
  }
47
47
  },
48
48
  "scripts": {
49
- "test": "node --check nodes/unifi-access-config.js && node --check nodes/unifi-access-device.js && node --check nodes/unifi-network-config.js && node --check nodes/unifi-network-control-poe.js && node --check nodes/unifi-network-device.js && node --check nodes/unifi-network-presence.js && node --check nodes/unifi-protect-config.js && node --check nodes/unifi-protect-device.js && node --check nodes/utils/unifi-access-utils.js && node --check nodes/utils/unifi-access-device-registry.js && node --check nodes/utils/unifi-network-utils.js && node --check nodes/utils/unifi-network-device-registry.js && node --check nodes/utils/unifi-protect-utils.js && node --check nodes/utils/unifi-protect-field-metadata.js && node --check nodes/utils/unifi-protect-device-registry.js && node --check scripts/unifi-registry-discovery-common.js && node --check scripts/discover-unifi-network-registry.js && node --check scripts/discover-unifi-protect-registry.js && node --check scripts/discover-unifi-access-registry.js",
49
+ "test": "node --check nodes/unifi-access-config.js && node --check nodes/unifi-access-device.js && node --check nodes/unifi-network-config.js && node --check nodes/unifi-network-control-poe.js && node --check nodes/unifi-network-device.js && node --check nodes/unifi-network-presence.js && node --check nodes/unifi-protect-config.js && node --check nodes/unifi-protect-device.js && node --check nodes/utils/http-response-utils.js && node --check nodes/utils/unifi-access-utils.js && node --check nodes/utils/unifi-access-device-registry.js && node --check nodes/utils/unifi-network-utils.js && node --check nodes/utils/unifi-network-device-registry.js && node --check nodes/utils/unifi-protect-utils.js && node --check nodes/utils/unifi-protect-field-metadata.js && node --check nodes/utils/unifi-protect-device-registry.js && node --check scripts/unifi-registry-discovery-common.js && node --check scripts/discover-unifi-network-registry.js && node --check scripts/discover-unifi-protect-registry.js && node --check scripts/discover-unifi-access-registry.js",
50
50
  "registry:discover:network": "node scripts/discover-unifi-network-registry.js",
51
51
  "registry:discover:protect": "node scripts/discover-unifi-protect-registry.js",
52
52
  "registry:discover:access": "node scripts/discover-unifi-access-registry.js"