Skip to content

Commit 823cedf

Browse files
committed
make lstat in filesystem interface optional to avoid breaking change
support options argument in SyncAsyncFileSystem CacheBackend should be able to cope with optional methods
1 parent acb5c1b commit 823cedf

File tree

4 files changed

+44
-38
lines changed

4 files changed

+44
-38
lines changed

lib/CachedInputFileSystem.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ class CacheBackend {
141141
/** @type {number | undefined} */
142142
this._nextDecay = undefined;
143143

144-
this.provide = this.provide.bind(this);
145-
this.provideSync = this.provideSync.bind(this);
144+
this.provide = provider ? this.provide.bind(this) : null;
145+
this.provideSync = syncProvider ? this.provideSync.bind(this) : null;
146146
}
147147

148148
provide(path, options, callback) {
@@ -462,6 +462,7 @@ module.exports = class CachedInputFileSystem {
462462

463463
purge(what) {
464464
this._statBackend.purge(what);
465+
this._lstatBackend.purge(what);
465466
this._readdirBackend.purgeParent(what);
466467
this._readFileBackend.purge(what);
467468
this._readlinkBackend.purge(what);

lib/Resolver.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +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
54+
* @property {(function(string, FileSystemCallback<FileSystemStats>): void) & function(string, object, FileSystemCallback<Buffer | string>): void=} lstat
5555
* @property {(function(string, FileSystemCallback<FileSystemStats>): void) & function(string, object, FileSystemCallback<Buffer | string>): void} stat
5656
*/
5757

@@ -61,7 +61,7 @@ const {
6161
* @property {function(string, object=): (Buffer | string)[] | FileSystemDirent[]} readdirSync
6262
* @property {(function(string, object=): object)=} readJsonSync
6363
* @property {function(string, object=): Buffer | string} readlinkSync
64-
* @property {function(string, object=): FileSystemStats} lstatSync
64+
* @property {function(string, object=): FileSystemStats=} lstatSync
6565
* @property {function(string, object=): FileSystemStats} statSync
6666
*/
6767

lib/SyncAsyncFileSystemDecorator.js

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,76 +15,81 @@
1515
function SyncAsyncFileSystemDecorator(fs) {
1616
this.fs = fs;
1717

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);
18+
this.lstat = undefined;
19+
this.lstatSync = undefined;
20+
const lstatSync = fs.lstatSync;
21+
if (lstatSync) {
22+
this.lstat = (arg, options, callback) => {
23+
let result;
24+
try {
25+
result = lstatSync.call(fs, arg);
26+
} catch (e) {
27+
return (callback || options)(e);
28+
}
29+
(callback || options)(null, result);
30+
};
31+
this.lstatSync = (arg, options) => lstatSync.call(fs, arg, options);
32+
}
2833

29-
this.stat = (arg, callback) => {
34+
this.stat = (arg, options, callback) => {
3035
let result;
3136
try {
32-
result = fs.statSync(arg);
37+
result = fs.statSync(arg, options);
3338
} catch (e) {
34-
return callback(e);
39+
return (callback || options)(e);
3540
}
36-
callback(null, result);
41+
(callback || options)(null, result);
3742
};
38-
this.statSync = arg => fs.statSync(arg);
43+
this.statSync = (arg, options) => fs.statSync(arg, options);
3944

40-
this.readdir = (arg, callback) => {
45+
this.readdir = (arg, options, callback) => {
4146
let result;
4247
try {
4348
result = fs.readdirSync(arg);
4449
} catch (e) {
45-
return callback(e);
50+
return (callback || options)(e);
4651
}
47-
callback(null, result);
52+
(callback || options)(null, result);
4853
};
49-
this.readdirSync = arg => fs.readdirSync(arg);
54+
this.readdirSync = (arg, options) => fs.readdirSync(arg, options);
5055

51-
this.readFile = (arg, callback) => {
56+
this.readFile = (arg, options, callback) => {
5257
let result;
5358
try {
5459
result = fs.readFileSync(arg);
5560
} catch (e) {
56-
return callback(e);
61+
return (callback || options)(e);
5762
}
58-
callback(null, result);
63+
(callback || options)(null, result);
5964
};
60-
this.readFileSync = arg => fs.readFileSync(arg);
65+
this.readFileSync = (arg, options) => fs.readFileSync(arg, options);
6166

62-
this.readlink = (arg, callback) => {
67+
this.readlink = (arg, options, callback) => {
6368
let result;
6469
try {
6570
result = fs.readlinkSync(arg);
6671
} catch (e) {
67-
return callback(e);
72+
return (callback || options)(e);
6873
}
69-
callback(null, result);
74+
(callback || options)(null, result);
7075
};
71-
this.readlinkSync = arg => fs.readlinkSync(arg);
76+
this.readlinkSync = (arg, options) => fs.readlinkSync(arg, options);
7277

7378
this.readJson = undefined;
7479
this.readJsonSync = undefined;
7580
const readJsonSync = fs.readJsonSync;
7681
if (readJsonSync) {
77-
this.readJson = (arg, callback) => {
82+
this.readJson = (arg, options, callback) => {
7883
let result;
7984
try {
8085
result = readJsonSync.call(fs, arg);
8186
} catch (e) {
82-
return callback(e);
87+
return (callback || options)(e);
8388
}
84-
callback(null, result);
89+
(callback || options)(null, result);
8590
};
8691

87-
this.readJsonSync = arg => readJsonSync.call(fs, arg);
92+
this.readJsonSync = (arg, options) => readJsonSync.call(fs, arg, options);
8893
}
8994
}
9095
module.exports = SyncAsyncFileSystemDecorator;

types.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ declare interface BaseResolveRequest {
1818
declare class CachedInputFileSystem {
1919
constructor(fileSystem?: any, duration?: any);
2020
fileSystem: any;
21-
lstat: {
21+
lstat?: {
2222
(arg0: string, arg1: FileSystemCallback<FileSystemStats>): void;
2323
(arg0: string, arg1: any, arg2: FileSystemCallback<string | Buffer>): void;
2424
};
25-
lstatSync: (arg0: string, arg1?: any) => FileSystemStats;
25+
lstatSync?: (arg0: string, arg1?: any) => FileSystemStats;
2626
stat: {
2727
(arg0: string, arg1: FileSystemCallback<FileSystemStats>): void;
2828
(arg0: string, arg1: any, arg2: FileSystemCallback<string | Buffer>): void;
@@ -90,7 +90,7 @@ declare interface FileSystem {
9090
(arg0: string, arg1: FileSystemCallback<string | Buffer>): void;
9191
(arg0: string, arg1: any, arg2: FileSystemCallback<string | Buffer>): void;
9292
};
93-
lstat: {
93+
lstat?: {
9494
(arg0: string, arg1: FileSystemCallback<FileSystemStats>): void;
9595
(arg0: string, arg1: any, arg2: FileSystemCallback<string | Buffer>): void;
9696
};

0 commit comments

Comments
 (0)