Skip to content

Commit 3643452

Browse files
committed
fix: correctly call fs.statSync when there are no options
- previously if the second parameter was a callback, it'd be passed as options to fs.statSync - who would ignore it, but wouldn't be fast about it (see below) - also makes performance on-par with enhanced-resolve 5.1.0 which in some benchmarks is about 3x faster than versions >= 5.2.0 || <= 5.8.0
1 parent c4aa24d commit 3643452

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

lib/SyncAsyncFileSystemDecorator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function SyncAsyncFileSystemDecorator(fs) {
3434
this.stat = (arg, options, callback) => {
3535
let result;
3636
try {
37-
result = fs.statSync(arg, options);
37+
result = callback ? fs.statSync(arg, options) : fs.statSync(arg);
3838
} catch (e) {
3939
return (callback || options)(e);
4040
}

test/SyncAsyncFileSystemDecorator.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
const should = require("should");
6+
const SyncAsyncFileSystemDecorator = require("../lib/SyncAsyncFileSystemDecorator");
7+
8+
describe("SyncAsyncFileSystemDecorator stat", function () {
9+
it("should use options when they're provided", function (done) {
10+
const decoratedFs = new SyncAsyncFileSystemDecorator(fs);
11+
decoratedFs.stat(
12+
path.join(__dirname, "fixtures", "decorated-fs", "exists.js"),
13+
{ bigint: true },
14+
function (error, result) {
15+
should(error).be.null();
16+
should(result).have.properties(["size", "birthtime"]);
17+
should(result.size).be.of.type("bigint");
18+
done();
19+
}
20+
);
21+
});
22+
23+
it("should work correctly when no options provided", function (done) {
24+
const decoratedFs = new SyncAsyncFileSystemDecorator(fs);
25+
decoratedFs.stat(
26+
path.join(__dirname, "fixtures", "decorated-fs", "exists.js"),
27+
function (error, result) {
28+
should(error).be.null();
29+
should(result).have.properties(["size", "birthtime"]);
30+
should(result.size).be.of.type("number");
31+
done();
32+
}
33+
);
34+
});
35+
});

test/fixtures/decorated-fs/exists.js

Whitespace-only changes.

0 commit comments

Comments
 (0)