node-red-contrib-unifi-ultimate 0.1.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.
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+
3
+ const http = require("http");
4
+ const https = require("https");
5
+
6
+ function normalizeHost(value) {
7
+ let host = String(value || "").trim();
8
+ if (!host) {
9
+ return "";
10
+ }
11
+
12
+ host = host.replace(/^https?:\/\//i, "");
13
+ host = host.replace(/\/.*$/, "");
14
+ return host.trim().replace(/\/+$/, "");
15
+ }
16
+
17
+ function buildBaseUrlFromHost(value) {
18
+ const host = normalizeHost(value);
19
+ if (!host) {
20
+ return "";
21
+ }
22
+ return `https://${host}/proxy/protect/integration`;
23
+ }
24
+
25
+ function buildQueryString(query) {
26
+ if (!query || typeof query !== "object" || Array.isArray(query)) {
27
+ return "";
28
+ }
29
+
30
+ const params = new URLSearchParams();
31
+
32
+ Object.entries(query).forEach(([key, value]) => {
33
+ if (value === undefined || value === null || value === "") {
34
+ return;
35
+ }
36
+ if (Array.isArray(value)) {
37
+ value.forEach((entry) => params.append(key, String(entry)));
38
+ return;
39
+ }
40
+ params.append(key, String(value));
41
+ });
42
+
43
+ const rendered = params.toString();
44
+ return rendered ? `?${rendered}` : "";
45
+ }
46
+
47
+ function maybeParseBody(contentType, buffer) {
48
+ const raw = buffer.toString("utf8");
49
+ if (!raw) {
50
+ return raw;
51
+ }
52
+
53
+ const trimmed = raw.trim();
54
+ const isJsonType = typeof contentType === "string" && contentType.includes("application/json");
55
+ const looksJson = trimmed.startsWith("{") || trimmed.startsWith("[");
56
+
57
+ if (isJsonType || looksJson) {
58
+ try {
59
+ return JSON.parse(trimmed);
60
+ } catch (error) {
61
+ return raw;
62
+ }
63
+ }
64
+
65
+ return raw;
66
+ }
67
+
68
+ function doRequest(url, options, body) {
69
+ return new Promise((resolve, reject) => {
70
+ const transport = url.protocol === "http:" ? http : https;
71
+ const requestOptions = {
72
+ protocol: url.protocol,
73
+ hostname: url.hostname,
74
+ port: url.port || undefined,
75
+ path: `${url.pathname}${url.search}`,
76
+ method: options.method,
77
+ headers: options.headers,
78
+ timeout: options.timeout,
79
+ rejectUnauthorized: options.rejectUnauthorized
80
+ };
81
+
82
+ const req = transport.request(requestOptions, (res) => {
83
+ const chunks = [];
84
+
85
+ res.on("data", (chunk) => chunks.push(chunk));
86
+ res.on("end", () => {
87
+ const buffer = Buffer.concat(chunks);
88
+ resolve({
89
+ statusCode: res.statusCode || 0,
90
+ headers: res.headers,
91
+ payload: maybeParseBody(res.headers["content-type"], buffer)
92
+ });
93
+ });
94
+ });
95
+
96
+ req.on("timeout", () => {
97
+ req.destroy(new Error("Request timed out"));
98
+ });
99
+
100
+ req.on("error", reject);
101
+
102
+ if (body !== undefined && body !== null) {
103
+ req.write(body);
104
+ }
105
+
106
+ req.end();
107
+ });
108
+ }
109
+
110
+ function buildRequestHeaders(authHeader, apiKey, msgHeaders) {
111
+ return Object.assign(
112
+ {
113
+ Accept: "application/json",
114
+ [authHeader]: apiKey
115
+ },
116
+ msgHeaders && typeof msgHeaders === "object" ? msgHeaders : {}
117
+ );
118
+ }
119
+
120
+ function buildRequestBody(headers, method, payload) {
121
+ if (method === "GET" || method === "HEAD" || payload === undefined) {
122
+ return undefined;
123
+ }
124
+
125
+ let requestBody;
126
+ if (Buffer.isBuffer(payload)) {
127
+ requestBody = payload;
128
+ } else if (typeof payload === "string") {
129
+ requestBody = payload;
130
+ if (!headers["Content-Type"] && !headers["content-type"]) {
131
+ headers["Content-Type"] = "text/plain; charset=utf-8";
132
+ }
133
+ } else {
134
+ requestBody = JSON.stringify(payload);
135
+ if (!headers["Content-Type"] && !headers["content-type"]) {
136
+ headers["Content-Type"] = "application/json";
137
+ }
138
+ }
139
+
140
+ if (!headers["Content-Length"] && !headers["content-length"]) {
141
+ headers["Content-Length"] = Buffer.byteLength(requestBody);
142
+ }
143
+
144
+ return requestBody;
145
+ }
146
+
147
+ module.exports = {
148
+ buildBaseUrlFromHost,
149
+ buildQueryString,
150
+ doRequest,
151
+ buildRequestHeaders,
152
+ buildRequestBody
153
+ };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "node-red-contrib-unifi-ultimate",
3
+ "version": "0.1.0-beta.1",
4
+ "description": "Node-RED nodes for Ubiquiti UniFi integrations, starting with UniFi Protect.",
5
+ "files": [
6
+ "nodes/",
7
+ "examples/",
8
+ "README.md",
9
+ "LICENSE"
10
+ ],
11
+ "keywords": [
12
+ "node-red",
13
+ "unifi",
14
+ "ubiquiti",
15
+ "protect",
16
+ "node-red-contrib"
17
+ ],
18
+ "dependencies": {
19
+ "ws": "8.20.0"
20
+ },
21
+ "license": "MIT",
22
+ "author": "Massimo Saccani",
23
+ "homepage": "https://github.com/massimosaccani/node-red-contrib-unifi-ultimate",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/massimosaccani/node-red-contrib-unifi-ultimate.git"
27
+ },
28
+ "engines": {
29
+ "node": ">=18.0.0"
30
+ },
31
+ "node-red": {
32
+ "version": ">=3.1.1",
33
+ "nodes": {
34
+ "unifi-protect-config": "nodes/unifi-protect-config.js",
35
+ "unifi-protect-device": "nodes/unifi-protect-device.js"
36
+ }
37
+ },
38
+ "scripts": {
39
+ "test": "node --check nodes/unifi-protect-config.js && node --check nodes/unifi-protect-device.js && node --check nodes/utils/unifi-protect-utils.js && node --check nodes/utils/unifi-protect-device-registry.js"
40
+ }
41
+ }