From 4abc696f853ef55a0dc55bb1969a66343404dc0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Wed, 31 Jan 2018 16:33:18 +0100 Subject: [PATCH] Ignore non-object package.json files, fixes #134 Previously this would hang. Node also ignores package.json files that don't contain objects. --- index.js | 2 +- test/invalid_pkg.js | 16 ++++++++++++++++ test/invalid_pkg/file.js | 1 + test/invalid_pkg/index.js | 1 + test/invalid_pkg/package.json | 1 + 5 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/invalid_pkg.js create mode 100644 test/invalid_pkg/file.js create mode 100644 test/invalid_pkg/index.js create mode 100644 test/invalid_pkg/package.json diff --git a/index.js b/index.js index 65170cc..1787b96 100644 --- a/index.js +++ b/index.js @@ -540,7 +540,7 @@ Deps.prototype.lookupPackage = function (file, cb) { fns.forEach(function (f) { f(err, pkg) }); } if (err) cb(err) - else if (pkg) cb(null, pkg) + else if (pkg && typeof pkg === 'object') cb(null, pkg) else { self.pkgCache[pkgfile] = false; next(); diff --git a/test/invalid_pkg.js b/test/invalid_pkg.js new file mode 100644 index 0000000..9435632 --- /dev/null +++ b/test/invalid_pkg.js @@ -0,0 +1,16 @@ +var mdeps = require('../'); +var test = require('tap').test; +var path = require('path'); +var fs = require('fs'); + +test('invalid pkg', function (t) { + var d = mdeps(); + d.on('package', function (pkg_) { +// console.error({pkg_}); + }); + d.end(path.join(__dirname, '/invalid_pkg/file.js')); + d.on('data', function () {}); + d.on('end', function () { + t.end(); + }); +}); diff --git a/test/invalid_pkg/file.js b/test/invalid_pkg/file.js new file mode 100644 index 0000000..9d59a3b --- /dev/null +++ b/test/invalid_pkg/file.js @@ -0,0 +1 @@ +require('./') diff --git a/test/invalid_pkg/index.js b/test/invalid_pkg/index.js new file mode 100644 index 0000000..27aff7f --- /dev/null +++ b/test/invalid_pkg/index.js @@ -0,0 +1 @@ +T.pass() diff --git a/test/invalid_pkg/package.json b/test/invalid_pkg/package.json new file mode 100644 index 0000000..7deb8b1 --- /dev/null +++ b/test/invalid_pkg/package.json @@ -0,0 +1 @@ +"just a string"