Skip to content

Commit

Permalink
fix(watch): callback was losing context. Use explicit obj
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Apr 27, 2024
1 parent 71c4837 commit 73013bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).

### Unreleased

### [1.2.3] - 2024-04-26
### [1.2.4] - 2024-04-26

- reader: use path.sep instead of [\\/] to be more obvious
- fix(watch): callback was losing context. Use explicit obj
- fix(reader): use path.sep instead of [\\/] to be more obvious

### [1.2.2] - 2024-04-24

Expand Down Expand Up @@ -133,3 +134,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
[1.2.3]: https://github.com/haraka/haraka-config/releases/tag/v1.2.3
[1.2.0]: https://github.com/haraka/haraka-config/releases/tag/v1.2.0
[1.2.1]: https://github.com/haraka/haraka-config/releases/tag/v1.2.1
[1.2.4]: https://github.com/haraka/haraka-config/releases/tag/v1.2.4
24 changes: 14 additions & 10 deletions lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const enoent = { timer: false, files: [] }
const watchers = {}
const sedation_timers = {}

module.exports.ensure_enoent_timer = (reader) => {
const Watch = {}

Watch.ensure_enoent_timer = (reader) => {
if (enoent.timer) return
// Create timer
enoent.timer = setInterval(() => {
Expand All @@ -19,15 +21,15 @@ module.exports.ensure_enoent_timer = (reader) => {
watchers[file] = fs.watch(
file,
{ persistent: false },
this.onEvent(reader, file, args),
Watch.onEvent(reader, file, args),
)
})
}
}, 60 * 1000)
enoent.timer.unref() // don't block process exit
}

module.exports.file = (reader, name, type, cb, options) => {
Watch.file = (reader, name, type, cb, options) => {
// This works on all OS's, but watch_dir() above is preferred for Linux and
// Windows as it is far more efficient.
// NOTE: we need a fs.watch per file. It's impossible to watch non-existent
Expand All @@ -39,21 +41,21 @@ module.exports.file = (reader, name, type, cb, options) => {
watchers[name] = fs.watch(
name,
{ persistent: false },
this.onEvent(reader, name, { type, options, cb }),
Watch.onEvent(reader, name, { type, options, cb }),
)
} catch (e) {
if (e.code === 'ENOENT') {
// ignore error when ENOENT
enoent.files[name] = true
this.ensure_enoent_timer(reader)
Watch.ensure_enoent_timer(reader)
} else {
console.error(`Error watching config file: ${name} : ${e}`)
}
}
}

// used to watch main haraka config dir
module.exports.dir = (reader) => {
Watch.dir = (reader) => {
// NOTE: Has OS platform limitations:
// https://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener
const cp = reader.config_path
Expand Down Expand Up @@ -82,7 +84,7 @@ module.exports.dir = (reader) => {
}

// used by getDir
module.exports.dir2 = (reader, dirPath) => {
Watch.dir2 = (reader, dirPath) => {
if (watchers[dirPath]) return
const watchOpts = { persistent: false, recursive: true }

Expand All @@ -105,7 +107,7 @@ module.exports.dir2 = (reader, dirPath) => {
})
}

module.exports.onEvent = (reader, name, args) => {
Watch.onEvent = (reader, name, args) => {
return (fse) => {
if (sedation_timers[name]) {
clearTimeout(sedation_timers[name])
Expand All @@ -126,15 +128,17 @@ module.exports.onEvent = (reader, name, args) => {
watchers[name] = fs.watch(
name,
{ persistent: false },
this.onEvent(...arguments),
Watch.onEvent(reader, name, args),
)
} catch (e) {
if (e.code === 'ENOENT') {
enoent.files[name] = true
this.ensure_enoent_timer(reader)
Watch.ensure_enoent_timer(reader)
} else {
console.error(`Error watching file: ${name} : ${e}`)
}
}
}
}

module.exports = Watch
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "haraka-config",
"license": "MIT",
"description": "Haraka's config file loader",
"version": "1.2.3",
"version": "1.2.4",
"homepage": "http://haraka.github.io",
"repository": {
"type": "git",
Expand Down

0 comments on commit 73013bb

Please sign in to comment.