Skip to content

Commit 9458928

Browse files
committed
[Breaking] ignore SyntaxErrors from readPackage/readPackageSync, eg due to a malformed package.json
This isn't really breaking, but I'm going to treat it as such, out of an abundance of caution.
1 parent 5288e64 commit 9458928

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

lib/async.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var defaultReadPackage = function defaultReadPackage(readFile, pkgfile, cb) {
5050
var pkg = JSON.parse(body);
5151
cb(null, pkg);
5252
} catch (jsonErr) {
53-
cb(null);
53+
cb(jsonErr);
5454
}
5555
}
5656
});
@@ -233,7 +233,7 @@ module.exports = function resolve(x, options, callback) {
233233
if (!ex) return loadpkg(path.dirname(dir), cb);
234234

235235
readPackage(readFile, pkgfile, function (err, pkgParam) {
236-
if (err) cb(err);
236+
if (err && !(err instanceof SyntaxError)) cb(err);
237237

238238
var pkg = pkgParam;
239239

lib/sync.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ var maybeRealpathSync = function maybeRealpathSync(realpathSync, x, opts) {
4646
};
4747

4848
var defaultReadPackageSync = function defaultReadPackageSync(readFileSync, pkgfile) {
49-
var body = readFileSync(pkgfile);
50-
try {
51-
var pkg = JSON.parse(body);
52-
return pkg;
53-
} catch (jsonErr) {}
49+
return JSON.parse(readFileSync(pkgfile));
5450
};
5551

5652
var getPackageCandidates = function getPackageCandidates(x, start, opts) {
@@ -145,7 +141,14 @@ module.exports = function resolveSync(x, options) {
145141
return loadpkg(path.dirname(dir));
146142
}
147143

148-
var pkg = readPackageSync(readFileSync, pkgfile);
144+
var pkg;
145+
try {
146+
pkg = readPackageSync(readFileSync, pkgfile);
147+
} catch (e) {
148+
if (!(e instanceof SyntaxError)) {
149+
throw e;
150+
}
151+
}
149152

150153
if (pkg && opts.packageFilter) {
151154
pkg = opts.packageFilter(pkg, pkgfile, dir);

readme.markdown

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ options are:
7878
* `opts.readPackage(readFile, pkgfile, cb)` - function to asynchronously read and parse a package.json file
7979
* readFile - the passed `opts.readFile` or `fs.readFile` if not specified
8080
* pkgfile - path to package.json
81-
* cb - callback
81+
* cb - callback. a SyntaxError error argument will be ignored, all other error arguments will be treated as an error.
8282

8383
* `opts.packageFilter(pkg, pkgfile, dir)` - transform the parsed package.json contents before looking at the "main" field
8484
* pkg - package data
@@ -152,7 +152,7 @@ default `opts` values:
152152
var pkg = JSON.parse(body);
153153
cb(null, pkg);
154154
} catch (jsonErr) {
155-
cb(null);
155+
cb(jsonErr);
156156
}
157157
}
158158
});
@@ -183,7 +183,7 @@ options are:
183183

184184
* opts.realpathSync - function to synchronously resolve a potential symlink to its real path
185185

186-
* `opts.readPackageSync(readFileSync, pkgfile)` - function to synchronously read and parse a package.json file
186+
* `opts.readPackageSync(readFileSync, pkgfile)` - function to synchronously read and parse a package.json file. a thrown SyntaxError will be ignored, all other exceptions will propagate.
187187
* readFileSync - the passed `opts.readFileSync` or `fs.readFileSync` if not specified
188188
* pkgfile - path to package.json
189189

@@ -256,11 +256,7 @@ default `opts` values:
256256
return file;
257257
},
258258
readPackageSync: function defaultReadPackageSync(readFileSync, pkgfile) {
259-
var body = readFileSync(pkgfile);
260-
try {
261-
var pkg = JSON.parse(body);
262-
return pkg;
263-
} catch (jsonErr) {}
259+
return JSON.parse(readFileSync(pkgfile));
264260
},
265261
moduleDirectory: 'node_modules',
266262
preserveSymlinks: false

test/resolver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ test('malformed package.json', function (t) {
572572
cb(null, JSON.parse(result));
573573
} catch (e) {
574574
t.ok(e instanceof SyntaxError, 'readPackage: malformed package.json parses as a syntax error');
575-
cb(null);
575+
cb(e);
576576
}
577577
});
578578
}

test/resolver_sync.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ test('malformed package.json', function (t) {
424424
return JSON.parse(result);
425425
} catch (e) {
426426
t.ok(e instanceof SyntaxError, 'readPackageSync: malformed package.json parses as a syntax error');
427+
throw e;
427428
}
428429
}
429430
}

0 commit comments

Comments
 (0)