@flowfuse/nr-mqtt-nodes 0.1.0
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/.eslintrc +21 -0
- package/.github/dependabot.yml +15 -0
- package/.github/workflows/project-automation.yml +10 -0
- package/.github/workflows/publish.yml +51 -0
- package/.github/workflows/release-publish.yml +21 -0
- package/CHANGELOG.md +3 -0
- package/LICENSE +178 -0
- package/README.md +29 -0
- package/nodes/ff-mqtt.html +488 -0
- package/nodes/ff-mqtt.js +1826 -0
- package/nodes/icons/ff-logo.svg +6 -0
- package/nodes/lib/TeamBrokerApi.js +83 -0
- package/nodes/lib/proxyHelper.js +301 -0
- package/nodes/lib/util.js +112 -0
- package/nodes/locales/en-US/ff-mqtt.html +121 -0
- package/nodes/locales/en-US/ff-mqtt.json +136 -0
- package/npmignore +5 -0
- package/package.json +54 -0
- package/test/unit/ff-mqtt_spec.js +920 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<svg width="90" height="135" version="1.1" viewBox="0 0 90 135" xmlns="http://www.w3.org/2000/svg">
|
|
3
|
+
<g transform="matrix(1.0078 0 0 1.0078 -67.007 -65.914)">
|
|
4
|
+
<path d="m77.77 92.7c-3.497 0-6.311 2.816-6.311 6.313-0.0052 11.55-0.01316 23.11-0.01499 34.66 7.5 0.0444 15.01 0.217 22.49-0.3183 10.43-0.9511 19.61-6.552 29.22-10.23 8.742-3.697 18.13-5.941 27.66-5.741l-1e-3 -18.38c4e-3 -3.498-2.815-6.313-6.311-6.313zm73.05 37.19c-1.055 0.0169-2.111 0.0457-3.166 0.0889-11.64-0.0448-21.96 5.974-32.46 10.19 8.115 3.283 15.95 7.454 24.55 9.389 3.675 0.5576 7.372 0.8292 11.08 0.9085zm-71.13 16.57c-2.747 6e-3 -5.494 0.0346-8.239 0.0486 0.0024 6.416 0.0072 12.83 0.01757 19.25 0.0037 3.498 2.815 6.313 6.311 6.313h66.73c3.497 0 6.311-2.816 6.311-6.313v-2.778c-8.203-0.0537-16.4-1.304-24.04-4.374-11.82-4.124-22.85-11.45-35.68-11.94-3.798-0.1794-7.602-0.2147-11.41-0.2062z" fill="#fff"/>
|
|
5
|
+
</g>
|
|
6
|
+
</svg>
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} LinkResult
|
|
3
|
+
* @property {string} id - The ID of the broker client
|
|
4
|
+
* @property {string} username - The username for the broker client
|
|
5
|
+
* @property {Array} acls - The access control lists for the broker client
|
|
6
|
+
* @property {Object} owner - The owner of the broker client
|
|
7
|
+
* @property {string} owner.instanceType - The type of instance (e.g., 'hosted')
|
|
8
|
+
* @property {string} owner.id - The ID of the owner instance
|
|
9
|
+
* @property {string} owner.name - The name of the owner instance
|
|
10
|
+
* @property {string} password - The password for the broker client
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates an API for interacting with the FlowFuse platform
|
|
15
|
+
* @param {Object} RED - The Node-RED runtime object
|
|
16
|
+
* @param {import('got').Got} gotClient - The got client to use for making HTTP requests
|
|
17
|
+
* @param {Object} options - Configuration parameters
|
|
18
|
+
* @param {string} options.forgeURL - The Forge URL
|
|
19
|
+
* @param {string} options.teamId - The team ID
|
|
20
|
+
* @param {string} options.token - The authentication token
|
|
21
|
+
* @param {string} [options.API_VERSION='v1'] - The API version to use
|
|
22
|
+
* @example
|
|
23
|
+
* const { TeamBrokerApi } = require('./lib/TeamBrokerApi.js');
|
|
24
|
+
* const got = require('got').default;
|
|
25
|
+
* const forgeURL = 'https://example.com/forge';
|
|
26
|
+
* const teamBrokerApi = TeamBrokerApi(RED, got, { forgeURL, teamId: "abcdef", token: "your-token" });
|
|
27
|
+
*/
|
|
28
|
+
function TeamBrokerApi (gotClient, { forgeURL, teamId, instanceType, instanceId, token, API_VERSION = 'v1', API_TIMEOUT = 5000 } = {}) {
|
|
29
|
+
const teamClientUserId = `${instanceType}:${instanceId}`
|
|
30
|
+
const baseURL = `${forgeURL}/api/${API_VERSION}/teams/${teamId}/broker/client`
|
|
31
|
+
const got = gotClient.extend({
|
|
32
|
+
headers: {
|
|
33
|
+
Authorization: `Bearer ${token}`
|
|
34
|
+
},
|
|
35
|
+
timeout: {
|
|
36
|
+
request: API_TIMEOUT
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Get the broker client user information
|
|
42
|
+
* @returns {Promise<Object>} - The broker client information
|
|
43
|
+
*/
|
|
44
|
+
async function getClient () {
|
|
45
|
+
const url = `${baseURL}/${teamClientUserId}`
|
|
46
|
+
const res = await got.get(url)
|
|
47
|
+
if (res.statusCode !== 200) {
|
|
48
|
+
throw new Error(`Failed to fetch client: ${res.statusCode} ${res.statusMessage}`)
|
|
49
|
+
}
|
|
50
|
+
const data = JSON.parse(res.body)
|
|
51
|
+
return data
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Link this instance to a broker client
|
|
56
|
+
* @param {string} password - The password to use for linking
|
|
57
|
+
* @returns {Promise<LinkResult>} - Returns broker settings
|
|
58
|
+
* @throws {Error} - Throws an error if the request fails
|
|
59
|
+
*/
|
|
60
|
+
async function link (password) {
|
|
61
|
+
const url = `${baseURL}/${teamClientUserId}/link`
|
|
62
|
+
console.debug(`Linking instance to broker client via URL: ${url}`)
|
|
63
|
+
const res = await got.post(url, {
|
|
64
|
+
json: {
|
|
65
|
+
password
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
if (res.statusCode !== 200 && res.statusCode !== 201) {
|
|
69
|
+
throw new Error(`Failed to link instances: ${res.statusCode} ${res.statusMessage}`)
|
|
70
|
+
}
|
|
71
|
+
const data = JSON.parse(res.body)
|
|
72
|
+
return data
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
getClient,
|
|
77
|
+
link
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
module.exports = {
|
|
82
|
+
TeamBrokerApi
|
|
83
|
+
}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This is a fork of the MQTT nodes from Node-RED.
|
|
3
|
+
The original code can be found at:
|
|
4
|
+
https://github.com/node-red/node-red/blob/283f7f5992b139200825f57234faddb409d7fcc9/packages/node_modules/@node-red/nodes/core/network/lib/proxyHelper.js
|
|
5
|
+
Below is the copyright notice for the original code.
|
|
6
|
+
The copyright notice for this fork is the same as the original.
|
|
7
|
+
### Changes:
|
|
8
|
+
- Hide advanced features
|
|
9
|
+
- Remove the config node for MQTT broker
|
|
10
|
+
- Remove the dynamic connection control
|
|
11
|
+
- lint errors fixed up
|
|
12
|
+
*/
|
|
13
|
+
/*
|
|
14
|
+
The MIT License
|
|
15
|
+
|
|
16
|
+
Copyright (C) 2016-2018 Rob Wu <rob@robwu.nl>
|
|
17
|
+
|
|
18
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
19
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
20
|
+
the Software without restriction, including without limitation the rights to
|
|
21
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
22
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
23
|
+
so, subject to the following conditions:
|
|
24
|
+
|
|
25
|
+
The above copyright notice and this permission notice shall be included in all
|
|
26
|
+
copies or substantial portions of the Software.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/*
|
|
30
|
+
This proxy helper is heavily based on the proxy helper from Rob Wu as detailed above.
|
|
31
|
+
It has been modified to work with the Node-RED runtime environment.
|
|
32
|
+
The license for the original code is reproduced above.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Parse a URL into its components.
|
|
37
|
+
* @param {String} url The URL to parse
|
|
38
|
+
* @returns {URL}
|
|
39
|
+
*/
|
|
40
|
+
const parseUrl = (url) => {
|
|
41
|
+
let parsedUrl = {
|
|
42
|
+
protocol: null,
|
|
43
|
+
host: null,
|
|
44
|
+
port: null,
|
|
45
|
+
hostname: null,
|
|
46
|
+
query: null,
|
|
47
|
+
href: null
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
if (!url) { return parsedUrl }
|
|
51
|
+
parsedUrl = new URL(url)
|
|
52
|
+
} catch (error) {
|
|
53
|
+
// dont throw error
|
|
54
|
+
}
|
|
55
|
+
return parsedUrl
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const DEFAULT_PORTS = {
|
|
59
|
+
ftp: 21,
|
|
60
|
+
gopher: 70,
|
|
61
|
+
http: 80,
|
|
62
|
+
https: 443,
|
|
63
|
+
ws: 80,
|
|
64
|
+
wss: 443,
|
|
65
|
+
mqtt: 1880,
|
|
66
|
+
mqtts: 8883
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const modeOverride = getEnv('NR_PROXY_MODE', {})
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @typedef {Object} ProxyOptions
|
|
73
|
+
* @property {'strict'|'legacy'} [mode] - Legacy mode is for non-strict previous proxy determination logic (for node-red <= v3.1 compatibility) (default 'strict')
|
|
74
|
+
* @property {boolean} [favourUpperCase] - Favour UPPER_CASE *_PROXY env vars (default false)
|
|
75
|
+
* @property {boolean} [lowerCaseOnly] - Prevent UPPER_CASE *_PROXY env vars being used. (default false)
|
|
76
|
+
* @property {boolean} [excludeNpm] - Prevent npm_config_*_proxy env vars being used. (default false)
|
|
77
|
+
* @property {object} [env] - The environment object to use (defaults to process.env)
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get the proxy URL for a given URL.
|
|
82
|
+
* @param {string|URL} url - The URL, or the result from url.parse.
|
|
83
|
+
* @param {ProxyOptions} [options] - The options object (optional)
|
|
84
|
+
* @return {string} The URL of the proxy that should handle the request to the
|
|
85
|
+
* given URL. If no proxy is set, this will be an empty string.
|
|
86
|
+
*/
|
|
87
|
+
function getProxyForUrl (url, options) {
|
|
88
|
+
url = url || ''
|
|
89
|
+
const defaultOptions = {
|
|
90
|
+
mode: 'strict',
|
|
91
|
+
lowerCaseOnly: false,
|
|
92
|
+
favourUpperCase: false,
|
|
93
|
+
excludeNpm: false
|
|
94
|
+
}
|
|
95
|
+
options = Object.assign({}, defaultOptions, options)
|
|
96
|
+
|
|
97
|
+
if (modeOverride === 'legacy' || modeOverride === 'strict') {
|
|
98
|
+
options.mode = modeOverride
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (options.mode === 'legacy') {
|
|
102
|
+
return legacyGetProxyForUrl(url, options.env || process.env)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const parsedUrl = typeof url === 'string' ? parseUrl(url) : url || {}
|
|
106
|
+
let proto = parsedUrl.protocol
|
|
107
|
+
let hostname = parsedUrl.host
|
|
108
|
+
let port = parsedUrl.port
|
|
109
|
+
if (typeof hostname !== 'string' || !hostname || typeof proto !== 'string') {
|
|
110
|
+
return '' // Don't proxy URLs without a valid scheme or host.
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
proto = proto.split(':', 1)[0]
|
|
114
|
+
// Stripping ports in this way instead of using parsedUrl.hostname to make
|
|
115
|
+
// sure that the brackets around IPv6 addresses are kept.
|
|
116
|
+
hostname = hostname.replace(/:\d*$/, '')
|
|
117
|
+
port = parseInt(port) || DEFAULT_PORTS[proto] || 0
|
|
118
|
+
if (!shouldProxy(hostname, port, options)) {
|
|
119
|
+
return '' // Don't proxy URLs that match NO_PROXY.
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
let proxy =
|
|
123
|
+
getEnv('npm_config_' + proto + '_proxy', options) ||
|
|
124
|
+
getEnv(proto + '_proxy', options) ||
|
|
125
|
+
getEnv('npm_config_proxy', options) ||
|
|
126
|
+
getEnv('all_proxy', options)
|
|
127
|
+
if (proxy && proxy.indexOf('://') === -1) {
|
|
128
|
+
// Missing scheme in proxy, default to the requested URL's scheme.
|
|
129
|
+
proxy = proto + '://' + proxy
|
|
130
|
+
}
|
|
131
|
+
return proxy
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Get the proxy URL for a given URL.
|
|
136
|
+
* For node-red < v3.1 or compatibility mode
|
|
137
|
+
* @param {string} url The URL to check for proxying
|
|
138
|
+
* @param {object} [env] The environment object to use (default process.env)
|
|
139
|
+
* @returns
|
|
140
|
+
*/
|
|
141
|
+
function legacyGetProxyForUrl (url, env) {
|
|
142
|
+
env = env || process.env
|
|
143
|
+
let prox, noprox
|
|
144
|
+
if (env.http_proxy) { prox = env.http_proxy }
|
|
145
|
+
if (env.HTTP_PROXY) { prox = env.HTTP_PROXY }
|
|
146
|
+
if (env.no_proxy) { noprox = env.no_proxy.split(',') }
|
|
147
|
+
if (env.NO_PROXY) { noprox = env.NO_PROXY.split(',') }
|
|
148
|
+
|
|
149
|
+
let noproxy = false
|
|
150
|
+
if (noprox) {
|
|
151
|
+
for (const i in noprox) {
|
|
152
|
+
if (url.indexOf(noprox[i].trim()) !== -1) { noproxy = true }
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (prox && !noproxy) {
|
|
156
|
+
return prox
|
|
157
|
+
}
|
|
158
|
+
return ''
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Determines whether a given URL should be proxied.
|
|
163
|
+
*
|
|
164
|
+
* @param {string} hostname - The host name of the URL.
|
|
165
|
+
* @param {number} port - The effective port of the URL.
|
|
166
|
+
* @returns {boolean} Whether the given URL should be proxied.
|
|
167
|
+
* @private
|
|
168
|
+
*/
|
|
169
|
+
function shouldProxy (hostname, port, options) {
|
|
170
|
+
const NO_PROXY =
|
|
171
|
+
(getEnv('npm_config_no_proxy', options) || getEnv('no_proxy', options)).toLowerCase()
|
|
172
|
+
if (!NO_PROXY) {
|
|
173
|
+
return true // Always proxy if NO_PROXY is not set.
|
|
174
|
+
}
|
|
175
|
+
if (NO_PROXY === '*') {
|
|
176
|
+
return false // Never proxy if wildcard is set.
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return NO_PROXY.split(/[,\s]/).every(function (proxy) {
|
|
180
|
+
if (!proxy) {
|
|
181
|
+
return true // Skip zero-length hosts.
|
|
182
|
+
}
|
|
183
|
+
const parsedProxy = proxy.match(/^(.+):(\d+)$/)
|
|
184
|
+
let parsedProxyHostname = parsedProxy ? parsedProxy[1] : proxy
|
|
185
|
+
const parsedProxyPort = parsedProxy ? parseInt(parsedProxy[2]) : 0
|
|
186
|
+
if (parsedProxyPort && parsedProxyPort !== port) {
|
|
187
|
+
return true // Skip if ports don't match.
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (!/^[.*]/.test(parsedProxyHostname)) {
|
|
191
|
+
// No wildcards, so stop proxying if there is an exact match.
|
|
192
|
+
return hostname !== parsedProxyHostname
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (parsedProxyHostname.charAt(0) === '*') {
|
|
196
|
+
// Remove leading wildcard.
|
|
197
|
+
parsedProxyHostname = parsedProxyHostname.slice(1)
|
|
198
|
+
}
|
|
199
|
+
// Stop proxying if the hostname ends with the no_proxy host.
|
|
200
|
+
return !hostname.endsWith(parsedProxyHostname)
|
|
201
|
+
})
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Get the value for an environment constiable.
|
|
206
|
+
*
|
|
207
|
+
* @param {string} key - The name of the environment constiable.
|
|
208
|
+
* @param {ProxyOptions} options - The name of the environment constiable.
|
|
209
|
+
* @return {string} The value of the environment constiable.
|
|
210
|
+
* @private
|
|
211
|
+
*/
|
|
212
|
+
function getEnv (key, options) {
|
|
213
|
+
const env = (options && options.env) || process.env
|
|
214
|
+
if (options && options.excludeNpm === true) {
|
|
215
|
+
if (key.startsWith('npm_config_')) {
|
|
216
|
+
return ''
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (options && options.lowerCaseOnly === true) {
|
|
220
|
+
return env[key.toLowerCase()] || ''
|
|
221
|
+
} else if (options && options.favourUpperCase === true) {
|
|
222
|
+
return env[key.toUpperCase()] || env[key.toLowerCase()] || ''
|
|
223
|
+
}
|
|
224
|
+
return env[key.toLowerCase()] || env[key.toUpperCase()] || ''
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Get a specific proxy agent for a WebSocket connection. This should be applied to the `wsOptions.agent` property
|
|
229
|
+
*
|
|
230
|
+
* NOTE: This utility function is specifically designed for the MQTT instances where the proxy is set based on the http based EndPoint
|
|
231
|
+
* that the instance will use to make a connection. As such, the proxy URL is determined based on the `wsEndPoint` provided in
|
|
232
|
+
* conjunction with env vars `http_proxy`, `https_proxy` and `no_proxy`.
|
|
233
|
+
*
|
|
234
|
+
* More Info:
|
|
235
|
+
* `wsOptions.agent` is expected to be an HTTP or HTTPS agent based on the request protocol
|
|
236
|
+
* http/ws requests use env var `http_proxy` and the HttpProxyAgent
|
|
237
|
+
* https/wss requests use env var `https_proxy` and the HttpsProxyAgent
|
|
238
|
+
* REF: https://github.com/TooTallNate/proxy-agents/tree/main/packages/proxy-agent#maps-proxy-protocols-to-httpagent-implementations
|
|
239
|
+
*
|
|
240
|
+
* @param {String} url - WebSocket url
|
|
241
|
+
* @param {import('http').AgentOptions} proxyOptions - proxy options
|
|
242
|
+
* @returns {import('https-proxy-agent').HttpsProxyAgent | import('http-proxy-agent').HttpProxyAgent | null}
|
|
243
|
+
*/
|
|
244
|
+
function getWSProxyAgent (url, proxyOptions) {
|
|
245
|
+
if (!url) {
|
|
246
|
+
return null
|
|
247
|
+
}
|
|
248
|
+
const _url = new URL(url)
|
|
249
|
+
const isHTTPBased = _url.protocol === 'ws:' || _url.protocol === 'http:'
|
|
250
|
+
const isHTTPSBased = _url.protocol === 'wss:' || _url.protocol === 'https:'
|
|
251
|
+
if (!isHTTPBased && !isHTTPSBased) {
|
|
252
|
+
return null
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// replace ^ws with http so that getProxyForUrl can return the correct http*_proxy for ws/wss
|
|
256
|
+
const proxyUrl = getProxyForUrl(url.replace(/^ws/, 'http'))
|
|
257
|
+
|
|
258
|
+
if (proxyUrl && isHTTPSBased) {
|
|
259
|
+
const HttpsAgent = require('https-proxy-agent').HttpsProxyAgent
|
|
260
|
+
return new HttpsAgent(proxyUrl, proxyOptions)
|
|
261
|
+
}
|
|
262
|
+
if (proxyUrl && isHTTPBased) {
|
|
263
|
+
const HttpAgent = require('http-proxy-agent').HttpProxyAgent
|
|
264
|
+
return new HttpAgent(proxyUrl, proxyOptions)
|
|
265
|
+
}
|
|
266
|
+
return null
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Get proxy agent for HTTP or HTTPS got instance. This should be applied to the `agent` property of the got instance options
|
|
271
|
+
*
|
|
272
|
+
* NOTE: This utility function is specifically designed for the GOT instances where the proxy is set based on the `httpEndPoint`
|
|
273
|
+
* that the instance will use to make requests. As such, the proxy URL is determined based on the `httpEndPoint` provided
|
|
274
|
+
* in conjunction with env vars `http_proxy`, `https_proxy` and `no_proxy`.
|
|
275
|
+
* @param {String} url - http or https URL
|
|
276
|
+
* @param {import('http').AgentOptions} proxyOptions - proxy options
|
|
277
|
+
* @returns {{http: import('http-proxy-agent').HttpProxyAgent | undefined, https: import('https-proxy-agent').HttpsProxyAgent | undefined}}
|
|
278
|
+
*/
|
|
279
|
+
function getHTTPProxyAgent (url, proxyOptions) {
|
|
280
|
+
const agent = {}
|
|
281
|
+
if (url) {
|
|
282
|
+
const _url = new URL(url)
|
|
283
|
+
const proxyUrl = getProxyForUrl(url)
|
|
284
|
+
if (proxyUrl && _url.protocol === 'http:') {
|
|
285
|
+
const HttpAgent = require('http-proxy-agent').HttpProxyAgent
|
|
286
|
+
agent.http = new HttpAgent(proxyUrl, proxyOptions)
|
|
287
|
+
}
|
|
288
|
+
if (proxyUrl && _url.protocol === 'https:') {
|
|
289
|
+
const HttpsAgent = require('https-proxy-agent').HttpsProxyAgent
|
|
290
|
+
agent.https = new HttpsAgent(proxyUrl, proxyOptions)
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return agent
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
module.exports = {
|
|
297
|
+
getProxyForUrl,
|
|
298
|
+
parseUrl,
|
|
299
|
+
getWSProxyAgent,
|
|
300
|
+
getHTTPProxyAgent
|
|
301
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses an nr-mqtt Node userId or clientId auth string into its components.
|
|
3
|
+
* @param {String} id - the ID to parse, expected format: `mq:hosted:teamId:instanceId[:haId]` or `mq:remote:teamId:deviceId[:projectId]`
|
|
4
|
+
* @param {'username'|'clientId'} [kind='username'] - the kind of ID to parse `'username'` or `'clientId'`
|
|
5
|
+
* @returns Parsed ID object
|
|
6
|
+
*/
|
|
7
|
+
function parseNrMqttId (id, kind = 'username') {
|
|
8
|
+
const validLengths = kind === 'clientId' ? [4, 5] : [4]
|
|
9
|
+
const result = {
|
|
10
|
+
teamId: null,
|
|
11
|
+
parentId: null,
|
|
12
|
+
parentType: null,
|
|
13
|
+
ownerId: null,
|
|
14
|
+
ownerType: null,
|
|
15
|
+
haId: null,
|
|
16
|
+
teamClientUsername: null
|
|
17
|
+
}
|
|
18
|
+
const parts = id.split(':')
|
|
19
|
+
if (!validLengths.includes(parts.length)) {
|
|
20
|
+
throw new Error(`Invalid ID format: ${id}`)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const [mq, instanceType, teamId, instanceId, part5] = parts
|
|
24
|
+
if (mq !== 'mq') {
|
|
25
|
+
throw new Error(`Invalid ID format: ${id}`)
|
|
26
|
+
}
|
|
27
|
+
result.teamId = teamId
|
|
28
|
+
result.ownerId = instanceId
|
|
29
|
+
if (instanceType === 'hosted') {
|
|
30
|
+
result.ownerType = 'instance'
|
|
31
|
+
result.parentType = 'application' // instances are treated as application owned
|
|
32
|
+
result.parentId = null // no application ID available at this point (or necessary)
|
|
33
|
+
result.teamClientUsername = `instance:${instanceId}`
|
|
34
|
+
if (kind === 'clientId' && parts.length === 4) {
|
|
35
|
+
// since a clientId can have a haId, we need to compute the teamClientUsername accordingly
|
|
36
|
+
result.haId = part5 || null
|
|
37
|
+
}
|
|
38
|
+
} else if (instanceType === 'remote') {
|
|
39
|
+
result.ownerType = 'device'
|
|
40
|
+
result.teamClientUsername = `device:${instanceId}`
|
|
41
|
+
result.parentType = 'application'
|
|
42
|
+
if (parts.length === 5) {
|
|
43
|
+
result.parentId = part5 // projectId
|
|
44
|
+
result.parentType = 'project'
|
|
45
|
+
} else {
|
|
46
|
+
result.parentId = null // no projectId for app owned devices
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
throw new Error('Invalid ID format')
|
|
50
|
+
}
|
|
51
|
+
return result
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Creates an MQTT Auth ID for a team instance nr-mqtt client.
|
|
56
|
+
* Auth ID is used to identify the broker client by type, team, id and optionally haId.
|
|
57
|
+
* - `mq:hosted:instanceId` for hosted instances
|
|
58
|
+
* - `mq:hosted:instanceId:haId` for hosted High Availability instances
|
|
59
|
+
* - `mq:remote:deviceId:projectId` for Instance Owned devices
|
|
60
|
+
* - `mq:remote:deviceId` for Application Owned devices
|
|
61
|
+
* @param {string} teamId - team hash ID
|
|
62
|
+
* @param {'device'|'project'|'instance'} instanceType - the type of instance
|
|
63
|
+
* @param {string} instanceId - the ID of the instance or device
|
|
64
|
+
* @returns {string} - The created MQTT Auth ID e.g. `mq:hosted:instanceId`
|
|
65
|
+
*/
|
|
66
|
+
function createNrMqttId (teamId, instanceType, instanceId) {
|
|
67
|
+
let _type = instanceType
|
|
68
|
+
if (instanceType === 'remote' || instanceType === 'device') {
|
|
69
|
+
_type = 'remote' // remote devices
|
|
70
|
+
} else if (instanceType === 'hosted' || instanceType === 'instance' || instanceType === 'project') {
|
|
71
|
+
_type = 'hosted' // hosted instances
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const parts = ['mq', _type, teamId, instanceId]
|
|
75
|
+
return parts.join(':')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Creates an MQTT Client ID for a team instance nr-mqtt client.
|
|
80
|
+
* Client ID is used to identify the broker client by type, team, id and optionally haId.
|
|
81
|
+
* @param {string} teamId - team hash ID
|
|
82
|
+
* @param {'device'|'instance'|'project'} instanceType - the type of instance (e.g., 'hosted', 'device')
|
|
83
|
+
* @param {string} instanceId - the ID of the instance or device
|
|
84
|
+
* @param {string} [haId=null] - optional High Availability instance ID
|
|
85
|
+
* @returns {string} - The created MQTT Client ID e.g. `mq:hosted:instanceId:haId`
|
|
86
|
+
*/
|
|
87
|
+
function createNrMqttClientId (teamId, instanceType, instanceId, haId = null) {
|
|
88
|
+
let mqttId = createNrMqttId(teamId, instanceType, instanceId)
|
|
89
|
+
if (haId && mqttId.startsWith('mq:hosted:')) {
|
|
90
|
+
mqttId += `:${haId}` // add haId for hosted instances
|
|
91
|
+
}
|
|
92
|
+
return mqttId
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Helper function to test an object has a property
|
|
97
|
+
* @param {object} obj Object to test
|
|
98
|
+
* @param {string} propName Name of property to find
|
|
99
|
+
* @returns true if object has property `propName`
|
|
100
|
+
*/
|
|
101
|
+
function hasProperty (obj, propName) {
|
|
102
|
+
// JavaScript does not protect the property name hasOwnProperty
|
|
103
|
+
// Object.prototype.hasOwnProperty.call is the recommended/safer test
|
|
104
|
+
return Object.prototype.hasOwnProperty.call(obj, propName)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
module.exports = {
|
|
108
|
+
parseNrMqttId,
|
|
109
|
+
createNrMqttId,
|
|
110
|
+
createNrMqttClientId,
|
|
111
|
+
hasProperty
|
|
112
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
This is a fork of the MQTT nodes from Node-RED.
|
|
3
|
+
The original code can be found at:
|
|
4
|
+
https://github.com/node-red/node-red/blob/9bf9b7a635aab7190c51b76f7aa3ede443da8f91/packages/node_modules/@node-red/nodes/core/network/10-mqtt.html
|
|
5
|
+
Below is the copyright notice for the original code.
|
|
6
|
+
The copyright notice for this fork is the same as the original.
|
|
7
|
+
### Changes:
|
|
8
|
+
- Hide advanced features
|
|
9
|
+
- Remove the config node for MQTT broker
|
|
10
|
+
- Remove the dynamic connection control
|
|
11
|
+
-->
|
|
12
|
+
<!--
|
|
13
|
+
Copyright JS Foundation and other contributors, http://js.foundation
|
|
14
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
15
|
+
you may not use this file except in compliance with the License.
|
|
16
|
+
You may obtain a copy of the License at
|
|
17
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
18
|
+
Unless required by applicable law or agreed to in writing, software
|
|
19
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
20
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
+
See the License for the specific language governing permissions and
|
|
22
|
+
limitations under the License.
|
|
23
|
+
-->
|
|
24
|
+
|
|
25
|
+
<script type="text/html" data-help-name="ff-mqtt-in">
|
|
26
|
+
<p>Automatically connects to the FlowFuse MQTT broker and subscribes to messages from the specified topic.</p>
|
|
27
|
+
<h3>Outputs</h3>
|
|
28
|
+
<dl class="message-properties">
|
|
29
|
+
<dt>payload <span class="property-type">string | buffer</span></dt>
|
|
30
|
+
<dd>a string unless detected as a binary buffer.</dd>
|
|
31
|
+
<dt>topic <span class="property-type">string</span></dt>
|
|
32
|
+
<dd>the MQTT topic, uses / as a hierarchy separator.</dd>
|
|
33
|
+
<dt>qos <span class="property-type">number</span> </dt>
|
|
34
|
+
<dd>0, fire and forget - 1, at least once - 2, once and once only.</dd>
|
|
35
|
+
<dt>retain <span class="property-type">boolean</span></dt>
|
|
36
|
+
<dd>true indicates the message was retained and may be old.</dd>
|
|
37
|
+
|
|
38
|
+
<dt class="optional">responseTopic <span class="property-type">string</span></dt>
|
|
39
|
+
<dd><b>MQTTv5</b>: the MQTT response topic for the message</dd>
|
|
40
|
+
<dt class="optional">correlationData <span class="property-type">Buffer</span></dt>
|
|
41
|
+
<dd><b>MQTTv5</b>: the correlation data for the message</dd>
|
|
42
|
+
<dt class="optional">contentType <span class="property-type">string</span></dt>
|
|
43
|
+
<dd><b>MQTTv5</b>: the content-type of the payload</dd>
|
|
44
|
+
<dt class="optional">userProperties <span class="property-type">object</span></dt>
|
|
45
|
+
<dd><b>MQTTv5</b>: any user properties of the message</dd>
|
|
46
|
+
<dt class="optional">messageExpiryInterval <span class="property-type">number</span></dt>
|
|
47
|
+
<dd><b>MQTTv5</b>: the expiry time, in seconds, of the message</dd>
|
|
48
|
+
</dl>
|
|
49
|
+
<h3>Details</h3>
|
|
50
|
+
The subscription topic can include MQTT wildcards, + for one level, # for multiple levels.</p>
|
|
51
|
+
<p>All FlowFuse MQTT nodes (in or out) share the same broker connection.</p>
|
|
52
|
+
<h4>Dynamic Subscription</h4>
|
|
53
|
+
The node can be configured to dynamically control the MQTT connection and its subscriptions. When
|
|
54
|
+
enabled, the node will have an input and can be controlled by passing it messages.
|
|
55
|
+
<h3>Inputs</h3>
|
|
56
|
+
<p>These only apply when the node has been configured for dynamic subscriptions.</p>
|
|
57
|
+
<dl class="message-properties">
|
|
58
|
+
<dt>action <span class="property-type">string</span></dt>
|
|
59
|
+
<dd>the name of the action the node should perform. Available actions are: <code>"connect"</code>,
|
|
60
|
+
<code>"disconnect"</code>, <code>"getSubscriptions"</code>, <code>"subscribe"</code> and
|
|
61
|
+
<code>"unsubscribe"</code>.</dd>
|
|
62
|
+
<dt class="optional">topic <span class="property-type">string|object|array</span></dt>
|
|
63
|
+
<dd>For the <code>"subscribe"</code> and <code>"unsubscribe"</code> actions, this property
|
|
64
|
+
provides the topic. It can be set as either:<ul>
|
|
65
|
+
<li>a String containing the topic filter</li>
|
|
66
|
+
<li>an Object containing <code>topic</code> and <code>qos</code> properties</li>
|
|
67
|
+
<li>an array of either strings or objects to handle multiple topics in one</li>
|
|
68
|
+
</ul>
|
|
69
|
+
</dd>
|
|
70
|
+
<i>NOTE: By default, the MQTT connection will be established using the settings provided by
|
|
71
|
+
the platform. These cannot be overridden by the node.</i>
|
|
72
|
+
</dl>
|
|
73
|
+
|
|
74
|
+
</script>
|
|
75
|
+
|
|
76
|
+
<script type="text/html" data-help-name="ff-mqtt-out">
|
|
77
|
+
<p>Automatically connects to the FlowFuse MQTT broker and publishes messages.</p>
|
|
78
|
+
<h3>Inputs</h3>
|
|
79
|
+
<dl class="message-properties">
|
|
80
|
+
<dt>payload <span class="property-type">string | buffer</span></dt>
|
|
81
|
+
<dd> the payload to publish. If this property is not set, no message will be sent. To send a blank message, set this property to an empty String.</dd>
|
|
82
|
+
<dt class="optional">topic <span class="property-type">string</span></dt>
|
|
83
|
+
<dd> the MQTT topic to publish to.</dd>
|
|
84
|
+
<dt class="optional">qos <span class="property-type">number</span></dt>
|
|
85
|
+
<dd>0, fire and forget - 1, at least once - 2, once and once only. Default 0.</dd>
|
|
86
|
+
<dt class="optional">retain <span class="property-type">boolean</span></dt>
|
|
87
|
+
<dd>set to true to retain the message on the broker. Default false.</dd>
|
|
88
|
+
<dt class="optional">responseTopic <span class="property-type">string</span></dt>
|
|
89
|
+
<dd><b>MQTTv5</b>: the MQTT response topic for the message</dd>
|
|
90
|
+
<dt class="optional">correlationData <span class="property-type">Buffer</span></dt>
|
|
91
|
+
<dd><b>MQTTv5</b>: the correlation data for the message</dd>
|
|
92
|
+
<dt class="optional">contentType <span class="property-type">string</span></dt>
|
|
93
|
+
<dd><b>MQTTv5</b>: the content-type of the payload</dd>
|
|
94
|
+
<dt class="optional">userProperties <span class="property-type">object</span></dt>
|
|
95
|
+
<dd><b>MQTTv5</b>: any user properties of the message</dd>
|
|
96
|
+
<dt class="optional">messageExpiryInterval <span class="property-type">number</span></dt>
|
|
97
|
+
<dd><b>MQTTv5</b>: the expiry time, in seconds, of the message</dd>
|
|
98
|
+
<dt class="optional">topicAlias <span class="property-type">number</span></dt>
|
|
99
|
+
<dd><b>MQTTv5</b>: the MQTT topic alias to use</dd>
|
|
100
|
+
</dl>
|
|
101
|
+
<h3>Details</h3>
|
|
102
|
+
<code>msg.payload</code> is used as the payload of the published message.
|
|
103
|
+
If it contains an Object it will be converted to a JSON string before being sent.
|
|
104
|
+
If it contains a binary Buffer the message will be published as-is.</p>
|
|
105
|
+
<p>The topic used can be configured in the node or, if left blank, can be set by <code>msg.topic</code>.</p>
|
|
106
|
+
<p>Likewise the QoS and retain values can be configured in the node or, if left
|
|
107
|
+
blank, set by <code>msg.qos</code> and <code>msg.retain</code> respectively. To clear a previously
|
|
108
|
+
retained topic from the broker, send a blank message to that topic with the retain flag set.</p>
|
|
109
|
+
<p>This node requires the connection to the FlowFuse broker to be configured.</p>
|
|
110
|
+
<p>All FlowFuse MQTT nodes (in or out) share the same broker connection.</p>
|
|
111
|
+
<h4>Dynamic Control</h4>
|
|
112
|
+
The connection shared by the node can be controlled dynamically. If the node receives
|
|
113
|
+
one of the following control messages, it will not publish the message payload as well.
|
|
114
|
+
<h3>Inputs</h3>
|
|
115
|
+
<dl class="message-properties">
|
|
116
|
+
<dt>action <span class="property-type">string</span></dt>
|
|
117
|
+
<dd>the name of the action the node should perform. Available actions are: <code>"connect"</code>,
|
|
118
|
+
and <code>"disconnect"</code>.</dd>
|
|
119
|
+
</dl>
|
|
120
|
+
|
|
121
|
+
</script>
|