node-red-contrib-chokidar-watcher 1.0.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.
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/chokidar-watcher/chokidar-watcher.html +50 -0
- package/chokidar-watcher/chokidar-watcher.js +75 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andres Carmona
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# node-red-contrib-chokidar-watcher
|
|
2
|
+
|
|
3
|
+
Node-RED node to watch folders using [chokidar](https://github.com/paulmillr/chokidar)
|
|
4
|
+
with **`awaitWriteFinish`** support: events fire only after a file has finished
|
|
5
|
+
being written.
|
|
6
|
+
|
|
7
|
+
This solves the classic problem of the core `watch` node (and inotify-based
|
|
8
|
+
watchers) firing while a file is still being uploaded — e.g. sensors pushing
|
|
9
|
+
data files over FTP, where you must not process a file until the transfer
|
|
10
|
+
completes.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
From the Node-RED editor: **Menu → Manage palette → Install** and search for
|
|
15
|
+
`node-red-contrib-chokidar-watcher`, or from your Node-RED user directory
|
|
16
|
+
(typically `~/.node-red`):
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install node-red-contrib-chokidar-watcher
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Drop the **chokidar-watcher** node into a flow and configure the folder to
|
|
25
|
+
watch. Every filesystem event emits a message:
|
|
26
|
+
|
|
27
|
+
```json
|
|
28
|
+
{
|
|
29
|
+
"payload": {
|
|
30
|
+
"type": "file",
|
|
31
|
+
"event": "add",
|
|
32
|
+
"path": "/data/incoming/sensor_data.xml",
|
|
33
|
+
"filename": "sensor_data.xml"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
- `type`: `file` or `directory`
|
|
39
|
+
- `event`: chokidar event (`add`, `change`, `unlink`, `addDir`, `unlinkDir`)
|
|
40
|
+
- `path`: full path of the affected file
|
|
41
|
+
- `filename`: basename
|
|
42
|
+
|
|
43
|
+
## Configuration
|
|
44
|
+
|
|
45
|
+
| Option | Default | Description |
|
|
46
|
+
|---|---|---|
|
|
47
|
+
| Folder | — | Path to watch (recursive) |
|
|
48
|
+
| Persistent | `true` | Keep the process alive while watching |
|
|
49
|
+
| Stability threshold (ms) | `2000` | File size must stay constant this long before the event fires (`awaitWriteFinish.stabilityThreshold`) |
|
|
50
|
+
| Polling interval (ms) | `100` | File size polling rate while waiting for stability (`awaitWriteFinish.pollInterval`) |
|
|
51
|
+
|
|
52
|
+
Initial scan events are ignored (`ignoreInitial: true`): only changes after
|
|
53
|
+
deploy are reported.
|
|
54
|
+
|
|
55
|
+
## Notes
|
|
56
|
+
|
|
57
|
+
- On network filesystems (CIFS/NFS) events may not propagate for writes made
|
|
58
|
+
from *other* hosts; writes made through the same mount (e.g. an FTP server
|
|
59
|
+
in the same pod/container) work fine.
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
MIT
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType('chokidar-watcher',{
|
|
3
|
+
category: 'storage',
|
|
4
|
+
color: '#C0DEED',
|
|
5
|
+
defaults: {
|
|
6
|
+
name: {value:""},
|
|
7
|
+
folder: {value:"", required:true},
|
|
8
|
+
persistent: {value:true},
|
|
9
|
+
awaitWriteStabilityThreshold: {value:2000},
|
|
10
|
+
awaitWritePollingInterval: {value:100}
|
|
11
|
+
},
|
|
12
|
+
inputs:0,
|
|
13
|
+
outputs:1,
|
|
14
|
+
icon: "file-in.png",
|
|
15
|
+
label: function() {
|
|
16
|
+
return this.name || this.folder || "chokidar-watcher";
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<script type="text/html" data-template-name="chokidar-watcher">
|
|
22
|
+
<div class="form-row">
|
|
23
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
24
|
+
<input type="text" id="node-input-name" placeholder="Name">
|
|
25
|
+
</div>
|
|
26
|
+
<div class="form-row">
|
|
27
|
+
<label for="node-input-folder"><i class="fa fa-folder"></i> Folder</label>
|
|
28
|
+
<input type="text" id="node-input-folder" placeholder="/path/to/folder">
|
|
29
|
+
</div>
|
|
30
|
+
<div class="form-row">
|
|
31
|
+
<label for="node-input-persistent"><i class="fa fa-refresh"></i> Persistent</label>
|
|
32
|
+
<input type="checkbox" id="node-input-persistent">
|
|
33
|
+
</div>
|
|
34
|
+
<div class="form-row">
|
|
35
|
+
<label for="node-input-awaitWriteStabilityThreshold">Stability (ms)</label>
|
|
36
|
+
<input type="number" id="node-input-awaitWriteStabilityThreshold" placeholder="2000">
|
|
37
|
+
</div>
|
|
38
|
+
<div class="form-row">
|
|
39
|
+
<label for="node-input-awaitWritePollingInterval">Poll interval (ms)</label>
|
|
40
|
+
<input type="number" id="node-input-awaitWritePollingInterval" placeholder="100">
|
|
41
|
+
</div>
|
|
42
|
+
</script>
|
|
43
|
+
|
|
44
|
+
<script type="text/html" data-help-name="chokidar-watcher">
|
|
45
|
+
<p>Watches a folder for file changes using chokidar with awaitWriteFinish support.</p>
|
|
46
|
+
<p><b>Folder</b>: The path to the folder to watch.</p>
|
|
47
|
+
<p><b>Persistent</b>: Whether the watcher should keep the process running.</p>
|
|
48
|
+
<p><b>Stability Threshold</b>: Time in ms to wait for a file to stop changing before considering it written.</p>
|
|
49
|
+
<p><b>Poll Interval</b>: Time in ms between polls when checking for file stability.</p>
|
|
50
|
+
</script>
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
module.exports = function(RED) {
|
|
2
|
+
var chokidar = require('chokidar');
|
|
3
|
+
var path = require('path');
|
|
4
|
+
var watchers = {};
|
|
5
|
+
|
|
6
|
+
function ChokidarWatcherNode(config) {
|
|
7
|
+
RED.nodes.createNode(this, config);
|
|
8
|
+
var node = this;
|
|
9
|
+
|
|
10
|
+
node.folder = config.folder;
|
|
11
|
+
node.persistent = config.persistent !== undefined ? config.persistent : true;
|
|
12
|
+
node.awaitWriteStabilityThreshold = config.awaitWriteStabilityThreshold || 2000;
|
|
13
|
+
node.awaitWritePollingInterval = config.awaitWritePollingInterval || 100;
|
|
14
|
+
|
|
15
|
+
if (watchers[node.id]) {
|
|
16
|
+
watchers[node.id].close();
|
|
17
|
+
delete watchers[node.id];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (!node.folder) {
|
|
21
|
+
node.warn("No folder specified");
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var watcherOptions = {
|
|
26
|
+
persistent: node.persistent,
|
|
27
|
+
ignoreInitial: true,
|
|
28
|
+
awaitWriteFinish: {
|
|
29
|
+
stabilityThreshold: node.awaitWriteStabilityThreshold,
|
|
30
|
+
pollInterval: node.awaitWritePollingInterval
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
var watcher = chokidar.watch(node.folder, watcherOptions);
|
|
35
|
+
|
|
36
|
+
watchers[node.id] = watcher;
|
|
37
|
+
|
|
38
|
+
node.status({fill: "green", shape: "dot", text: "watching"});
|
|
39
|
+
|
|
40
|
+
watcher.on('all', function(event, filePath) {
|
|
41
|
+
var type = 'file';
|
|
42
|
+
if (event === 'addDir' || event === 'unlinkDir') {
|
|
43
|
+
type = 'directory';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
var payload = {
|
|
47
|
+
type: type,
|
|
48
|
+
event: event,
|
|
49
|
+
path: filePath,
|
|
50
|
+
filename: path.basename(filePath)
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
node.send({payload: payload});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
watcher.on('error', function(error) {
|
|
57
|
+
node.error("Watcher error: " + error.message);
|
|
58
|
+
node.status({fill: "red", shape: "ring", text: "error"});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
watcher.on('ready', function() {
|
|
62
|
+
node.status({fill: "green", shape: "dot", text: "ready"});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
node.on('close', function() {
|
|
66
|
+
if (watchers[node.id]) {
|
|
67
|
+
watchers[node.id].close();
|
|
68
|
+
delete watchers[node.id];
|
|
69
|
+
}
|
|
70
|
+
node.status({});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
RED.nodes.registerType("chokidar-watcher", ChokidarWatcherNode);
|
|
75
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "node-red-contrib-chokidar-watcher",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Node-RED node to watch folders using chokidar with awaitWriteFinish support",
|
|
5
|
+
"author": "Andres Carmona <valdepeace@gmail.com> (https://www.valdepeace.com)",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/valdepeace/node-red-contrib-chokidar-watcher.git"
|
|
9
|
+
},
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/valdepeace/node-red-contrib-chokidar-watcher/issues"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://www.valdepeace.com",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"node-red",
|
|
16
|
+
"chokidar",
|
|
17
|
+
"watch",
|
|
18
|
+
"watcher",
|
|
19
|
+
"folder",
|
|
20
|
+
"file",
|
|
21
|
+
"inotify",
|
|
22
|
+
"awaitWriteFinish"
|
|
23
|
+
],
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"files": [
|
|
26
|
+
"chokidar-watcher",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=18"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"chokidar": "^3.5.3"
|
|
35
|
+
},
|
|
36
|
+
"node-red": {
|
|
37
|
+
"version": ">=3.0.0",
|
|
38
|
+
"nodes": {
|
|
39
|
+
"chokidar-watcher": "chokidar-watcher/chokidar-watcher.js"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|