@nrutils/node-red-utils 0.0.0-stage → 0.2.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/CHANGELOG.md +20 -0
- package/LICENSE +190 -0
- package/README.md +187 -2
- package/index.js +50 -0
- package/node-red-base/NodeRedNodeBase.js +143 -0
- package/node-red-base/README.md +215 -0
- package/node-red-editorcomms/EditorCommsBackend.js +155 -0
- package/node-red-editorcomms/EditorCommsFrontend.js +105 -0
- package/node-red-editorcomms/README.md +164 -0
- package/node-red-gpu/GpuBuffer.js +29 -0
- package/node-red-gpu/Kernel.js +35 -0
- package/node-red-gpu/README.md +220 -0
- package/node-red-gpu/backends/CpuBackend.js +109 -0
- package/node-red-gpu/backends/VulkanBackend.js +232 -0
- package/node-red-gpu/errors.js +48 -0
- package/node-red-gpu/index.js +6 -0
- package/node-red-gpu/log.js +18 -0
- package/node-red-gpu/native/binding.gyp +14 -0
- package/node-red-gpu/native/src/gpu_addon.cpp +1066 -0
- package/node-red-gpu/nodeRedGpuMain.js +39 -0
- package/node-red-gpu/nodeRedGpuWorker.js +242 -0
- package/node-red-gpu/transpiler/emitGlsl.js +497 -0
- package/node-red-gpu/transpiler/glslNames.js +60 -0
- package/node-red-gpu/transpiler/index.js +81 -0
- package/node-red-gpu/transpiler/inferTypes.js +302 -0
- package/node-red-gpu/transpiler/validate.js +461 -0
- package/node-red-popup/NodeRedPopupBackend.js +69 -0
- package/node-red-popup/NodeRedPopupFrontend.js +508 -0
- package/node-red-popup/README.md +113 -0
- package/node-red-pubsub-bus/NodeRedPubsubBus.js +617 -0
- package/node-red-pubsub-bus/NodeRedPubsubBusWorkerProxy.js +239 -0
- package/node-red-pubsub-bus/README.md +201 -0
- package/node-red-resourceendpoint/NodeRedResourceEndpoint.js +205 -0
- package/node-red-resourceendpoint/README.md +119 -0
- package/node-red-singleton/NodeRedSingletonBackend.js +30 -0
- package/node-red-singleton/NodeRedSingletonFrontend.js +54 -0
- package/node-red-singleton/README.md +72 -0
- package/node-red-worker/NodeRedWorker.js +443 -0
- package/node-red-worker/NodeRedWorkerRuntime.js +659 -0
- package/node-red-worker/README.md +273 -0
- package/node-red-worker/workerRuntimeLoader.js +55 -0
- package/package.json +70 -5
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.2.0 (2026-11-10)
|
|
4
|
+
|
|
5
|
+
- `nodeRedWorker`: a throw (or a rejected Promise) in a worker `subscribe` handler goes to the new `worker.handlerError(error, type, data)`, with the data of the message. The default reports it on the node with `node.error`, so a Catch node gets it, instead of `console.error`. The next handlers of the same message still run. A worker file can replace `worker.handlerError`.
|
|
6
|
+
- `nodeRedPopup`: the popup unfurls from its node with a spring animation. The user moves it by its status bar; the position is saved in `nru_popupConfig` (`x`, `y`) and a dashed line ties it to the node. A resize follows the zoom of the canvas. The popup is a rounded card in the editor theme (light or dark), with the color of its node on the top edge and the tether line, dots on the status bar that show it can be moved, and a larger resize corner.
|
|
7
|
+
|
|
8
|
+
## 0.1.0
|
|
9
|
+
|
|
10
|
+
First public release.
|
|
11
|
+
|
|
12
|
+
- `NodeRedNodeBase`: mixin composition.
|
|
13
|
+
- `nodeRedEditorComms`: editor and backend messaging.
|
|
14
|
+
- `nodeRedWorker`: worker threads, with `request()` / `handle()` for one answer per message.
|
|
15
|
+
- `nodeRedResourceEndpoint`: static file serving for the editor, also from `resources` in the `createNode` options.
|
|
16
|
+
- `nodeRedPopup`: a panel that opens when you click the node in the editor. Experimental.
|
|
17
|
+
- `nodeRedSingleton`: one node of a type in the editor. Experimental.
|
|
18
|
+
- `nodeRedPubsubBus`: pub/sub between nodes; consumers pull the current value.
|
|
19
|
+
- `nodeRedGpu`: Vulkan compute. Experimental and optional. Without the native addon, kernels run on the CPU backend.
|
|
20
|
+
- Eight examples (nine nodes) in `examples/`.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2024 Contributors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,188 @@
|
|
|
1
|
-
#
|
|
1
|
+
# node-red-utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@nrutils/node-red-utils)
|
|
4
|
+
|
|
5
|
+
A toolkit for Node-RED custom nodes: worker threads, GPU compute, messages between the editor and the runtime, pub/sub between nodes, editor resources, a popup and singleton nodes.
|
|
6
|
+
|
|
7
|
+
- [`NodeRedNodeBase`](./node-red-base/README.md): builds a node from a setup function and a list of mixins. A mixin is one feature; you add it by listing it.
|
|
8
|
+
- [`nodeRedWorker`](./node-red-worker/README.md): runs node code in a worker thread. Wired worker nodes talk thread to thread, the same as the wires in the flow.
|
|
9
|
+
- [`nodeRedGpu`](./node-red-gpu/README.md) (experimental): Vulkan compute kernels in worker threads, with a CPU fallback.
|
|
10
|
+
- [`nodeRedEditorComms`](./node-red-editorcomms/README.md): messages between the runtime node and the same node in the editor.
|
|
11
|
+
- [`nodeRedPubsubBus`](./node-red-pubsub-bus/README.md): pub/sub for data that nodes share without wires.
|
|
12
|
+
- [`nodeRedResourceEndpoint`](./node-red-resourceendpoint/README.md): serves scripts, styles and images to the editor.
|
|
13
|
+
- [`nodeRedPopup`](./node-red-popup/README.md) (experimental): a resizable panel that opens when you click the node in the editor.
|
|
14
|
+
- [`nodeRedSingleton`](./node-red-singleton/README.md) (experimental): allows only one node of a type in the editor.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
In your node package:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @nrutils/node-red-utils
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Works with Node-RED 4 and Node.js 18 or later.
|
|
25
|
+
|
|
26
|
+
## Building with AI
|
|
27
|
+
|
|
28
|
+
Use an AI coding assistant to build nodes! Here's some [AI dreamed up paste-ready prompts](./prompts/README.md). Not particularly useful nodes though.
|
|
29
|
+
|
|
30
|
+
## Example: a counter node
|
|
31
|
+
|
|
32
|
+
A node that counts messages, shows the count as its status, and resets the count when you click its button in the editor.
|
|
33
|
+
|
|
34
|
+
`counter.js`:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
const { NodeRedNodeBase, nodeRedEditorComms } = require('@nrutils/node-red-utils');
|
|
38
|
+
|
|
39
|
+
module.exports = NodeRedNodeBase.createNode('counter', [nodeRedEditorComms], (RED, thisNode, config) => {
|
|
40
|
+
let count = 0;
|
|
41
|
+
const show = () => thisNode.status({ text: `${count} messages` });
|
|
42
|
+
|
|
43
|
+
// Sent by the button in counter.html
|
|
44
|
+
thisNode.nodeRedEditorComms.subscribe('reset', () => {
|
|
45
|
+
count = 0;
|
|
46
|
+
show();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
thisNode.on('input', (msg, send, done) => {
|
|
50
|
+
count++;
|
|
51
|
+
show();
|
|
52
|
+
send(msg);
|
|
53
|
+
done();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`counter.html`:
|
|
59
|
+
|
|
60
|
+
```html
|
|
61
|
+
<script type="text/javascript" src="node-red-resourceendpoint/node-red-editorcomms/EditorCommsFrontend.js"></script>
|
|
62
|
+
|
|
63
|
+
<script type="text/javascript">
|
|
64
|
+
RED.nodes.registerType('counter', {
|
|
65
|
+
category: 'function',
|
|
66
|
+
color: '#a6bbcf',
|
|
67
|
+
defaults: { name: { value: '' } },
|
|
68
|
+
inputs: 1,
|
|
69
|
+
outputs: 1,
|
|
70
|
+
icon: 'function.svg',
|
|
71
|
+
label: function () { return this.name || 'counter'; },
|
|
72
|
+
button: {
|
|
73
|
+
onclick: function () {
|
|
74
|
+
this.nodeRedEditorComms.publish('reset', {})
|
|
75
|
+
.catch((err) => RED.notify(err.status === 404 ? 'Deploy the node first' : err.message, 'warning'));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
</script>
|
|
80
|
+
|
|
81
|
+
<script type="text/html" data-template-name="counter">
|
|
82
|
+
<div class="form-row">
|
|
83
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
84
|
+
<input type="text" id="node-input-name" placeholder="Name">
|
|
85
|
+
</div>
|
|
86
|
+
</script>
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The first `<script>` line loads the editor side of `nodeRedEditorComms`. Copy this line exactly. The library serves the file; there is nothing to install.
|
|
90
|
+
|
|
91
|
+
## Example: a worker node
|
|
92
|
+
|
|
93
|
+
A node that hashes each payload in a worker thread, so the Node-RED event loop does not wait.
|
|
94
|
+
|
|
95
|
+
`hash-node.js`:
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
const path = require('path');
|
|
99
|
+
const { NodeRedNodeBase, nodeRedWorker } = require('@nrutils/node-red-utils');
|
|
100
|
+
|
|
101
|
+
module.exports = NodeRedNodeBase.createNode('hash-node', [nodeRedWorker], (RED, thisNode, config) => {
|
|
102
|
+
const worker = thisNode.nodeRedWorker.init(path.join(__dirname, 'hash-worker.js'), { rounds: 1000 });
|
|
103
|
+
|
|
104
|
+
thisNode.on('input', (msg, send, done) => {
|
|
105
|
+
worker.request('hash', String(msg.payload)).then((digest) => {
|
|
106
|
+
msg.payload = digest;
|
|
107
|
+
send(msg);
|
|
108
|
+
done();
|
|
109
|
+
}, done);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`hash-worker.js`:
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
const crypto = require('crypto');
|
|
118
|
+
|
|
119
|
+
module.exports = function (worker, data) {
|
|
120
|
+
worker.handle('hash', (text) => {
|
|
121
|
+
let digest = text;
|
|
122
|
+
for (let i = 0; i < data.rounds; i++) digest = crypto.createHash('sha256').update(digest).digest('hex');
|
|
123
|
+
return digest;
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## package.json
|
|
129
|
+
|
|
130
|
+
```json
|
|
131
|
+
{
|
|
132
|
+
"name": "node-red-contrib-counter",
|
|
133
|
+
"version": "0.1.0",
|
|
134
|
+
"keywords": ["node-red"],
|
|
135
|
+
"node-red": {
|
|
136
|
+
"version": ">=4.0.0",
|
|
137
|
+
"nodes": { "counter": "counter.js" }
|
|
138
|
+
},
|
|
139
|
+
"dependencies": { "@nrutils/node-red-utils": "^0.1.0" }
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
- The `.html` file sits next to the `.js` file and has the same base name.
|
|
144
|
+
- The type string in the HTML `registerType` equals the type in `createNode`.
|
|
145
|
+
- Put the library under `dependencies`, not `devDependencies`.
|
|
146
|
+
|
|
147
|
+
To load your node package into Node-RED on your own computer, run `cd ~/.node-red && npm install /path/to/your-package`, then restart Node-RED.
|
|
148
|
+
|
|
149
|
+
## Reference
|
|
150
|
+
|
|
151
|
+
Put the features you want in the array of `createNode`; they apply in that order, then `setup(RED, thisNode, config)` runs.
|
|
152
|
+
|
|
153
|
+
| Mixin | What it adds on `thisNode` | Editor script `<script src=...>` line |
|
|
154
|
+
| --- | --- | --- |
|
|
155
|
+
| `nodeRedWorker` | `nodeRedWorker`; call `init()` to start the worker | none |
|
|
156
|
+
| `nodeRedGpu` | nothing; the GPU API is in the worker, from `nodeRedGpuWorker()` | none |
|
|
157
|
+
| `nodeRedEditorComms` | `nodeRedEditorComms` | `node-red-resourceendpoint/node-red-editorcomms/EditorCommsFrontend.js` |
|
|
158
|
+
| `nodeRedPubsubBus` | `nodeRedPubsubBus` | none |
|
|
159
|
+
| `nodeRedResourceEndpoint` | `nodeRedResourceEndpoint` | none |
|
|
160
|
+
| `nodeRedPopup` | nothing | `node-red-resourceendpoint/node-red-popup/NodeRedPopupFrontend.js` |
|
|
161
|
+
| `nodeRedSingleton` | nothing | `node-red-resourceendpoint/node-red-singleton/NodeRedSingletonFrontend.js` |
|
|
162
|
+
|
|
163
|
+
A mixin with an editor script must be in the array, or the script answers 404.
|
|
164
|
+
|
|
165
|
+
Exports: `NodeRedNodeBase` and the lowercase mixins above, plus:
|
|
166
|
+
|
|
167
|
+
- `getBus()`: the pub/sub bus of the process, for code that is not a node.
|
|
168
|
+
- `NodeRedResourceEndpoint`: serves files without a node instance; `new NodeRedResourceEndpoint(RED).registerResources(nodeType, paths)`.
|
|
169
|
+
|
|
170
|
+
To write your own mixin, see [node-red-base](./node-red-base/README.md#write-a-mixin).
|
|
171
|
+
|
|
172
|
+
## Examples
|
|
173
|
+
|
|
174
|
+
[`examples/basic-examples`](./examples/README.md) has one small node package for each feature. Install one with `cd ~/.node-red && npm install --install-links /path/to/node-red-utils/examples/basic-examples/<name>`, then restart Node-RED.
|
|
175
|
+
|
|
176
|
+
## Troubleshooting
|
|
177
|
+
|
|
178
|
+
- The editor script gets a 404 (for example `node-red-resourceendpoint/node-red-editorcomms/EditorCommsFrontend.js`, in the browser's network tab): the mixin is not in the `createNode` array of a node type that Node-RED loaded, or the `src` of the `<script>` tag starts with `/`. Restart Node-RED after you install or change a node package.
|
|
179
|
+
|
|
180
|
+
## Contributing
|
|
181
|
+
|
|
182
|
+
Issues and pull requests are welcome on [GitHub](https://github.com/UpDownMe733/node-red-utils/issues). Run `npm test` before you send a pull request.
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
[Apache-2.0](./LICENSE)
|
|
187
|
+
|
|
188
|
+
<small>[Sponsor this project](https://github.com/sponsors/UpDownMe733)</small>
|
package/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2024 Contributors
|
|
3
|
+
* Licensed under the Apache License, Version 2.0
|
|
4
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
const NodeRedNodeBase = require('./node-red-base/NodeRedNodeBase.js');
|
|
10
|
+
|
|
11
|
+
const NodeRedEditorComms = require('./node-red-editorcomms/EditorCommsBackend.js');
|
|
12
|
+
|
|
13
|
+
const NodeRedResourceEndpoint = require('./node-red-resourceendpoint/NodeRedResourceEndpoint.js');
|
|
14
|
+
|
|
15
|
+
const NodeRedWorker = require('./node-red-worker/NodeRedWorker.js');
|
|
16
|
+
const NodeRedWorkerRuntime = require('./node-red-worker/NodeRedWorkerRuntime.js');
|
|
17
|
+
|
|
18
|
+
const NodeRedPopup = require('./node-red-popup/NodeRedPopupBackend.js');
|
|
19
|
+
const NodeRedSingleton = require('./node-red-singleton/NodeRedSingletonBackend.js');
|
|
20
|
+
|
|
21
|
+
const NodeRedPubsubBus = require('./node-red-pubsub-bus/NodeRedPubsubBus.js');
|
|
22
|
+
|
|
23
|
+
module.exports = {
|
|
24
|
+
|
|
25
|
+
NodeRedNodeBase: NodeRedNodeBase.NodeRedNodeBase,
|
|
26
|
+
|
|
27
|
+
nodeRedEditorComms: NodeRedEditorComms.nodeRedEditorComms,
|
|
28
|
+
NodeRedEditorComms: NodeRedEditorComms.NodeRedEditorComms,
|
|
29
|
+
|
|
30
|
+
nodeRedResourceEndpoint: NodeRedResourceEndpoint.nodeRedResourceEndpoint,
|
|
31
|
+
NodeRedResourceEndpoint: NodeRedResourceEndpoint.NodeRedResourceEndpoint,
|
|
32
|
+
|
|
33
|
+
nodeRedWorker: NodeRedWorker.nodeRedWorker,
|
|
34
|
+
NodeRedWorker: NodeRedWorker.NodeRedWorker,
|
|
35
|
+
NodeRedWorkerRuntime: NodeRedWorkerRuntime.NodeRedWorkerRuntime,
|
|
36
|
+
|
|
37
|
+
nodeRedPopup: NodeRedPopup.nodeRedPopup,
|
|
38
|
+
NodeRedPopup: NodeRedPopup.NodeRedPopup,
|
|
39
|
+
|
|
40
|
+
nodeRedSingleton: NodeRedSingleton.nodeRedSingleton,
|
|
41
|
+
|
|
42
|
+
nodeRedPubsubBus: NodeRedPubsubBus.nodeRedPubsubBus,
|
|
43
|
+
getBus: NodeRedPubsubBus.getBus,
|
|
44
|
+
NodeRedPubsubBus: NodeRedPubsubBus.NodeRedPubsubBus,
|
|
45
|
+
|
|
46
|
+
// Loaded on first use, so that nodes without GPU code do not load the GPU module and acorn
|
|
47
|
+
get nodeRedGpu() { return require('./node-red-gpu/index.js').nodeRedGpu; },
|
|
48
|
+
get nodeRedGpuWorker() { return require('./node-red-gpu/index.js').nodeRedGpuWorker; },
|
|
49
|
+
|
|
50
|
+
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2024 Contributors
|
|
3
|
+
* Licensed under the Apache License, Version 2.0
|
|
4
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base class for Node-RED nodes that supports mixin composition
|
|
11
|
+
*
|
|
12
|
+
* This class serves as the foundation for creating Node-RED nodes
|
|
13
|
+
* and provides functionality for applying multiple mixins using
|
|
14
|
+
* a functional composition pattern.
|
|
15
|
+
*/
|
|
16
|
+
/** Short description of a value for error messages */
|
|
17
|
+
function describe(value) {
|
|
18
|
+
if (value === null) return 'null';
|
|
19
|
+
if (Array.isArray(value)) return 'an array';
|
|
20
|
+
if (typeof value === 'function') return `function ${value.name || '(anonymous)'}`;
|
|
21
|
+
return typeof value;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Throws a TypeError unless mixin is a function that takes a class and returns a subclass of it */
|
|
25
|
+
function checkMixin(mixin, i, where) {
|
|
26
|
+
const prefix = `${where}: mixins[${i}] must be a mixin function (superclass) => class extends superclass`;
|
|
27
|
+
if (typeof mixin !== 'function') throw new TypeError(`${prefix}; got ${describe(mixin)}`);
|
|
28
|
+
if (mixin.toString().startsWith('class ')) throw new TypeError(`${prefix}; got the class ${mixin.name || '(anonymous)'} itself`);
|
|
29
|
+
const TestClass = function() {};
|
|
30
|
+
let result;
|
|
31
|
+
try {
|
|
32
|
+
result = mixin(TestClass);
|
|
33
|
+
} catch (e) {
|
|
34
|
+
throw new TypeError(`${prefix}; calling it threw: ${e.message}`);
|
|
35
|
+
}
|
|
36
|
+
if (typeof result !== 'function' || !(result.prototype instanceof TestClass)) {
|
|
37
|
+
throw new TypeError(`${prefix}; it returned ${describe(result)}, not a subclass of its argument`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
class NodeRedNodeBase {
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Applies one or more mixin functions to extend the base class
|
|
45
|
+
*
|
|
46
|
+
* This method uses functional composition to progressively apply
|
|
47
|
+
* each mixin function to the class, allowing multiple behaviors
|
|
48
|
+
* to be combined without deep inheritance hierarchies.
|
|
49
|
+
*
|
|
50
|
+
* @param {...Function} mixins - One or more mixin functions that transform classes
|
|
51
|
+
* @returns {Class} - A new class with all mixins applied
|
|
52
|
+
* @throws {TypeError} - If any of the provided mixins is not a function
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* // Single mixin
|
|
56
|
+
* class MyNode extends NodeRedNodeBase.with(MixinA) {}
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // Multiple mixins (applied left to right)
|
|
60
|
+
* class MyNode extends NodeRedNodeBase.with(MixinA, MixinB, MixinC) {}
|
|
61
|
+
*/
|
|
62
|
+
static with(...mixins) {
|
|
63
|
+
mixins.forEach((mixin, i) => checkMixin(mixin, i, 'NodeRedNodeBase.with(...mixins)'));
|
|
64
|
+
return mixins.reduce((currentClass, mixin) => mixin(currentClass), this);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Creates a Node-RED node with specified mixins and custom setup
|
|
69
|
+
*
|
|
70
|
+
* @param {string} nodeType - The type name to register with Node-RED
|
|
71
|
+
* @param {Array} mixins - Array of mixin functions to apply
|
|
72
|
+
* @param {Function} setupFn - Function for node setup (receives RED, thisNode, config)
|
|
73
|
+
* @param {Object} [options] - Third argument of RED.nodes.registerType, for example { credentials, settings }.
|
|
74
|
+
* It can also hold `resources`: paths that the nodeRedResourceEndpoint mixin serves from the start.
|
|
75
|
+
* createNode does not pass `resources` on to registerType.
|
|
76
|
+
* @returns {Function} - A function to be exported by the node module
|
|
77
|
+
*/
|
|
78
|
+
static createNode(nodeType, mixins, setupFn, options) {
|
|
79
|
+
const where = 'createNode(type, mixins, setup)';
|
|
80
|
+
if (typeof nodeType !== 'string' || !nodeType) {
|
|
81
|
+
throw new TypeError(`${where}: type must be a non-empty string; got ${describe(nodeType)}`);
|
|
82
|
+
}
|
|
83
|
+
if (!Array.isArray(mixins)) {
|
|
84
|
+
throw new TypeError(`${where}: mixins must be an array of mixin functions; got ${describe(mixins)}`);
|
|
85
|
+
}
|
|
86
|
+
mixins.forEach((mixin, i) => checkMixin(mixin, i, where));
|
|
87
|
+
if (typeof setupFn !== 'function') {
|
|
88
|
+
throw new TypeError(`${where}: setup must be a function (RED, thisNode, config); got ${describe(setupFn)}`);
|
|
89
|
+
}
|
|
90
|
+
if (options !== undefined && (options === null || typeof options !== 'object' || Array.isArray(options))) {
|
|
91
|
+
throw new TypeError(`${where}: options must be an object such as { credentials, settings }; got ${describe(options)}`);
|
|
92
|
+
}
|
|
93
|
+
const { resources, ...registerOptions } = options || {};
|
|
94
|
+
return function(RED) {
|
|
95
|
+
// Create the composed class with mixins
|
|
96
|
+
const NodeClass = class extends NodeRedNodeBase.with(...mixins) {
|
|
97
|
+
// Read by onRegisterType of the resource endpoint mixin
|
|
98
|
+
static _registration = { nodeType, resources };
|
|
99
|
+
|
|
100
|
+
constructor(RED, thisNode, config) {
|
|
101
|
+
super(RED, thisNode, config);
|
|
102
|
+
|
|
103
|
+
// Run the setup function. It must be synchronous; report the error of an async one.
|
|
104
|
+
const result = setupFn(RED, thisNode, config);
|
|
105
|
+
if (result && typeof result.then === 'function') {
|
|
106
|
+
result.then(undefined, (err) => thisNode.error(`setup of ${nodeType} failed: ${(err && err.message) || err}`));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// Let the mixins do their one-time work before any node exists
|
|
112
|
+
NodeClass.onRegisterType(RED);
|
|
113
|
+
|
|
114
|
+
RED.nodes.registerType(nodeType, function(config) {
|
|
115
|
+
RED.nodes.createNode(this, config);
|
|
116
|
+
new NodeClass(RED, this, config);
|
|
117
|
+
}, options && registerOptions);
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Called one time when the node type is registered, before any node instance exists.
|
|
123
|
+
* A mixin that overrides it must call super.onRegisterType(RED).
|
|
124
|
+
* The editor mixins use it to serve their frontend scripts.
|
|
125
|
+
*
|
|
126
|
+
* @param {Object} RED - The Node-RED runtime object
|
|
127
|
+
*/
|
|
128
|
+
static onRegisterType(RED) {}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Creates a new Node-RED node
|
|
132
|
+
*
|
|
133
|
+
* @param {Object} RED - The Node-RED runtime object
|
|
134
|
+
* @param {Object} thisNode - The node being created
|
|
135
|
+
* @param {Object} config - Configuration options for the node
|
|
136
|
+
*/
|
|
137
|
+
constructor(RED, thisNode, config) {
|
|
138
|
+
// Base implementation to be extended by subclasses
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
module.exports.NodeRedNodeBase = NodeRedNodeBase;
|