diff --git a/package.json b/package.json index 4176967..7c32e45 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "debug": "^4.1.1", "human-interval": "^1.0.0", "is-string-and-not-blank": "^0.0.2", + "is-valid-path": "^0.1.1", "ms": "^2.1.2", "safe-timers": "^1.1.0" }, diff --git a/src/index.js b/src/index.js index f838f76..94dd131 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,7 @@ const cron = require('cron-validate'); const debug = require('debug')('bree'); const humanInterval = require('human-interval'); const isSANB = require('is-string-and-not-blank'); +const isValidPath = require('is-valid-path'); const later = require('@breejs/later'); const ms = require('ms'); const threads = require('bthreads'); @@ -103,7 +104,7 @@ class Bree extends EventEmitter { // validate root (sync check) if (isSANB(this.config.root)) { /* istanbul ignore next */ - if (hasFsStatSync) { + if (hasFsStatSync && isValidPath(this.config.root)) { const stats = fs.statSync(this.config.root); if (!stats.isDirectory()) throw new Error( @@ -176,6 +177,7 @@ class Bree extends EventEmitter { // eslint-disable-next-line complexity validateJob(job, i, names, errors) { + if (typeof this.config.jobs[i] === 'undefined') this.config.jobs[i] = {}; // support a simple string which we will transform to have a path if (isSANB(job)) { // don't allow a job to have the `index` file name @@ -215,7 +217,7 @@ class Bree extends EventEmitter { ); try { /* istanbul ignore next */ - if (hasFsStatSync) { + if (hasFsStatSync && isValidPath(path)) { const stats = fs.statSync(path); if (!stats.isFile()) throw new Error(`Job #${i + 1} "${job}" path missing: ${path}`); @@ -311,7 +313,7 @@ class Bree extends EventEmitter { if (path) { try { /* istanbul ignore next */ - if (hasFsStatSync) { + if (hasFsStatSync && isValidPath(path)) { const stats = fs.statSync(path); // eslint-disable-next-line max-depth if (!stats.isFile()) diff --git a/test/test.js b/test/test.js index b553e33..b2d4744 100644 --- a/test/test.js +++ b/test/test.js @@ -1656,3 +1656,10 @@ test('remove > fails if job does not exist', (t) => { t.throws(() => bree.remove('basic'), { message: /Job .* does not exist/ }); }); + +test('add > successfully adds job object', (t) => { + const bree = new Bree({ root: false }); + function noop() {} + bree.add({ name: 'basic', path: noop.toString() }); + t.pass(); +}); diff --git a/yarn.lock b/yarn.lock index ed39eec..49730fd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4619,6 +4619,11 @@ is-extendable@^1.0.1: dependencies: is-plain-object "^2.0.4" +is-extglob@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-1.0.0.tgz#ac468177c4943405a092fc8f29760c6ffc6206c0" + integrity sha1-rEaBd8SUNAWgkvyPKXYMb/xiBsA= + is-extglob@^2.1.0, is-extglob@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2" @@ -4642,6 +4647,13 @@ is-get-set-prop@^1.0.0: get-set-props "^0.1.0" lowercase-keys "^1.0.0" +is-glob@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-2.0.1.tgz#d096f926a3ded5600f3fdfd91198cb0888c2d863" + integrity sha1-0Jb5JqPe1WAPP9/ZEZjLCIjC2GM= + dependencies: + is-extglob "^1.0.0" + is-glob@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-3.1.0.tgz#7ba5ae24217804ac70707b96922567486cc3e84a" @@ -4674,6 +4686,13 @@ is-interactive@^1.0.0: resolved "https://registry.yarnpkg.com/is-interactive/-/is-interactive-1.0.0.tgz#cea6e6ae5c870a7b0a0004070b7b587e0252912e" integrity sha512-2HvIEKRoqS62guEC+qBjpvRubdX910WCMuJTZ+I9yvqKU2/12eSL549HMwtabb4oupdj2sMP50k+XJfB/8JE6w== +is-invalid-path@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/is-invalid-path/-/is-invalid-path-0.1.0.tgz#307a855b3cf1a938b44ea70d2c61106053714f34" + integrity sha1-MHqFWzzxqTi0TqcNLGEQYFNxTzQ= + dependencies: + is-glob "^2.0.0" + is-js-type@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/is-js-type/-/is-js-type-2.0.0.tgz#73617006d659b4eb4729bba747d28782df0f7e22" @@ -4838,6 +4857,13 @@ is-url@^1.2.2: resolved "https://registry.yarnpkg.com/is-url/-/is-url-1.2.4.tgz#04a4df46d28c4cff3d73d01ff06abeb318a1aa52" integrity sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww== +is-valid-path@^0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/is-valid-path/-/is-valid-path-0.1.1.tgz#110f9ff74c37f663e1ec7915eb451f2db93ac9df" + integrity sha1-EQ+f90w39mPh7HkV60UfLbk6yd8= + dependencies: + is-invalid-path "^0.1.0" + is-whitespace-character@^1.0.0: version "1.0.4" resolved "https://registry.yarnpkg.com/is-whitespace-character/-/is-whitespace-character-1.0.4.tgz#0858edd94a95594c7c9dd0b5c174ec6e45ee4aa7"