Skip to content

Commit acb5c1b

Browse files
feat: lstat and lstatSync
1 parent 4a3cc76 commit acb5c1b

File tree

5 files changed

+116
-1
lines changed

5 files changed

+116
-1
lines changed

lib/CachedInputFileSystem.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,17 @@ module.exports = class CachedInputFileSystem {
371371
constructor(fileSystem, duration) {
372372
this.fileSystem = fileSystem;
373373

374+
this._lstatBackend = createBackend(
375+
duration,
376+
this.fileSystem.lstat,
377+
this.fileSystem.lstatSync,
378+
this.fileSystem
379+
);
380+
const lstat = this._lstatBackend.provide;
381+
this.lstat = /** @type {FileSystem["lstat"]} */ (lstat);
382+
const lstatSync = this._lstatBackend.provideSync;
383+
this.lstatSync = /** @type {SyncFileSystem["lstatSync"]} */ (lstatSync);
384+
374385
this._statBackend = createBackend(
375386
duration,
376387
this.fileSystem.stat,

lib/Resolver.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const {
5151
* @property {(function(string, FileSystemCallback<(Buffer | string)[] | FileSystemDirent[]>): void) & function(string, object, FileSystemCallback<(Buffer | string)[] | FileSystemDirent[]>): void} readdir
5252
* @property {((function(string, FileSystemCallback<object>): void) & function(string, object, FileSystemCallback<object>): void)=} readJson
5353
* @property {(function(string, FileSystemCallback<Buffer | string>): void) & function(string, object, FileSystemCallback<Buffer | string>): void} readlink
54+
* @property {(function(string, FileSystemCallback<FileSystemStats>): void) & function(string, object, FileSystemCallback<Buffer | string>): void} lstat
5455
* @property {(function(string, FileSystemCallback<FileSystemStats>): void) & function(string, object, FileSystemCallback<Buffer | string>): void} stat
5556
*/
5657

@@ -60,6 +61,7 @@ const {
6061
* @property {function(string, object=): (Buffer | string)[] | FileSystemDirent[]} readdirSync
6162
* @property {(function(string, object=): object)=} readJsonSync
6263
* @property {function(string, object=): Buffer | string} readlinkSync
64+
* @property {function(string, object=): FileSystemStats} lstatSync
6365
* @property {function(string, object=): FileSystemStats} statSync
6466
*/
6567

lib/SyncAsyncFileSystemDecorator.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
*/
1515
function SyncAsyncFileSystemDecorator(fs) {
1616
this.fs = fs;
17+
18+
this.lstat = (arg, callback) => {
19+
let result;
20+
try {
21+
result = fs.lstatSync(arg);
22+
} catch (e) {
23+
return callback(e);
24+
}
25+
callback(null, result);
26+
};
27+
this.lstatSync = arg => fs.lstatSync(arg);
28+
1729
this.stat = (arg, callback) => {
1830
let result;
1931
try {

test/CachedInputFileSystem.js

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var should = require("should");
22

33
var { CachedInputFileSystem } = require("../");
44

5-
describe("CachedInputFileSystem OperationMergerBackend", function () {
5+
describe("CachedInputFileSystem OperationMergerBackend ('stat' and 'statSync')", function () {
66
this.timeout(3000);
77
var fs;
88

@@ -83,6 +83,87 @@ describe("CachedInputFileSystem OperationMergerBackend", function () {
8383
});
8484
});
8585

86+
describe("CachedInputFileSystem OperationMergerBackend ('lstat' and 'lstatSync')", function () {
87+
this.timeout(3000);
88+
var fs;
89+
90+
beforeEach(function () {
91+
fs = new CachedInputFileSystem(
92+
{
93+
lstat: function (path, options, callback) {
94+
if (!callback) {
95+
callback = options;
96+
options = undefined;
97+
}
98+
setTimeout(
99+
() =>
100+
callback(null, {
101+
path,
102+
options
103+
}),
104+
100
105+
);
106+
},
107+
lstatSync: function (path, options) {
108+
return {
109+
path,
110+
options
111+
};
112+
}
113+
},
114+
0
115+
);
116+
});
117+
afterEach(function () {
118+
fs.purge();
119+
});
120+
121+
it("should join accesses", function (done) {
122+
fs.lstat("a", function (err, result) {
123+
should.exist(result);
124+
result.a = true;
125+
});
126+
fs.lstat("a", function (err, result) {
127+
should.exist(result);
128+
should.exist(result.a);
129+
done();
130+
});
131+
});
132+
133+
it("should not join accesses with options", function (done) {
134+
fs.lstat("a", function (err, result) {
135+
should.exist(result);
136+
result.a = true;
137+
result.path.should.be.eql("a");
138+
should.not.exist(result.options);
139+
});
140+
fs.lstat("a", { options: true }, function (err, result) {
141+
should.exist(result);
142+
should.not.exist(result.a);
143+
result.path.should.be.eql("a");
144+
result.options.should.eql({ options: true });
145+
done();
146+
});
147+
});
148+
149+
it("should not cache accesses", function (done) {
150+
fs.lstat("a", function (err, result) {
151+
result.a = true;
152+
fs.lstat("a", function (err, result) {
153+
should.not.exist(result.a);
154+
done();
155+
});
156+
});
157+
});
158+
159+
it("should not cache sync accesses", function () {
160+
const result = fs.lstatSync("a");
161+
result.a = true;
162+
const result2 = fs.lstatSync("a");
163+
should.not.exist(result2.a);
164+
});
165+
});
166+
86167
describe("CachedInputFileSystem CacheBackend", function () {
87168
this.timeout(3000);
88169
var fs;

types.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ declare interface BaseResolveRequest {
1818
declare class CachedInputFileSystem {
1919
constructor(fileSystem?: any, duration?: any);
2020
fileSystem: any;
21+
lstat: {
22+
(arg0: string, arg1: FileSystemCallback<FileSystemStats>): void;
23+
(arg0: string, arg1: any, arg2: FileSystemCallback<string | Buffer>): void;
24+
};
25+
lstatSync: (arg0: string, arg1?: any) => FileSystemStats;
2126
stat: {
2227
(arg0: string, arg1: FileSystemCallback<FileSystemStats>): void;
2328
(arg0: string, arg1: any, arg2: FileSystemCallback<string | Buffer>): void;
@@ -85,6 +90,10 @@ declare interface FileSystem {
8590
(arg0: string, arg1: FileSystemCallback<string | Buffer>): void;
8691
(arg0: string, arg1: any, arg2: FileSystemCallback<string | Buffer>): void;
8792
};
93+
lstat: {
94+
(arg0: string, arg1: FileSystemCallback<FileSystemStats>): void;
95+
(arg0: string, arg1: any, arg2: FileSystemCallback<string | Buffer>): void;
96+
};
8897
stat: {
8998
(arg0: string, arg1: FileSystemCallback<FileSystemStats>): void;
9099
(arg0: string, arg1: any, arg2: FileSystemCallback<string | Buffer>): void;

0 commit comments

Comments
 (0)