-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
patchFs.ts
264 lines (224 loc) Β· 6.9 KB
/
patchFs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import fs from 'fs';
import {promisify} from 'util';
import {FakeFS} from './FakeFS';
import {URLFS} from './URLFS';
import {NativePath} from './path';
const SYNC_IMPLEMENTATIONS = new Set([
`accessSync`,
`appendFileSync`,
`createReadStream`,
`chmodSync`,
`chownSync`,
`closeSync`,
`copyFileSync`,
`linkSync`,
`lstatSync`,
`fstatSync`,
`lutimesSync`,
`mkdirSync`,
`openSync`,
`opendirSync`,
`readSync`,
`readlinkSync`,
`readFileSync`,
`readdirSync`,
`readlinkSync`,
`realpathSync`,
`renameSync`,
`rmdirSync`,
`statSync`,
`symlinkSync`,
`truncateSync`,
`unlinkSync`,
`unwatchFile`,
`utimesSync`,
`watch`,
`watchFile`,
`writeFileSync`,
`writeSync`,
]);
const ASYNC_IMPLEMENTATIONS = new Set([
`accessPromise`,
`appendFilePromise`,
`chmodPromise`,
`chownPromise`,
`closePromise`,
`copyFilePromise`,
`linkPromise`,
`fstatPromise`,
`lstatPromise`,
`lutimesPromise`,
`mkdirPromise`,
`openPromise`,
`opendirPromise`,
`readdirPromise`,
`realpathPromise`,
`readFilePromise`,
`readdirPromise`,
`readlinkPromise`,
`renamePromise`,
`rmdirPromise`,
`statPromise`,
`symlinkPromise`,
`truncatePromise`,
`unlinkPromise`,
`utimesPromise`,
`writeFilePromise`,
`writeSync`,
]);
const FILEHANDLE_IMPLEMENTATIONS = new Set([
`appendFilePromise`,
`chmodPromise`,
`chownPromise`,
`closePromise`,
`readPromise`,
`readFilePromise`,
`statPromise`,
`truncatePromise`,
`utimesPromise`,
`writePromise`,
`writeFilePromise`,
]);
export function patchFs(patchedFs: typeof fs, fakeFs: FakeFS<NativePath>): void {
// We wrap the `fakeFs` with a `URLFS` to add support for URL instances
fakeFs = new URLFS(fakeFs);
const setupFn = (target: any, name: string, replacement: any) => {
const orig = target[name];
target[name] = replacement;
// Preserve any util.promisify implementations
if (typeof orig?.[promisify.custom] !== `undefined`) {
replacement[promisify.custom] = orig[promisify.custom];
}
};
/** Callback implementations */
{
setupFn(patchedFs, `exists`, (p: string, ...args: Array<any>) => {
const hasCallback = typeof args[args.length - 1] === `function`;
const callback = hasCallback ? args.pop() : () => {};
process.nextTick(() => {
fakeFs.existsPromise(p).then(exists => {
callback(exists);
}, () => {
callback(false);
});
});
});
setupFn(patchedFs, `read`, (p: number, buffer: Buffer, ...args: Array<any>) => {
const hasCallback = typeof args[args.length - 1] === `function`;
const callback = hasCallback ? args.pop() : () => {};
process.nextTick(() => {
fakeFs.readPromise(p, buffer, ...args).then(bytesRead => {
callback(null, bytesRead, buffer);
}, error => {
callback(error);
});
});
});
for (const fnName of ASYNC_IMPLEMENTATIONS) {
const origName = fnName.replace(/Promise$/, ``);
if (typeof (patchedFs as any)[origName] === `undefined`)
continue;
const fakeImpl: Function = (fakeFs as any)[fnName];
if (typeof fakeImpl === `undefined`)
continue;
const wrapper = (...args: Array<any>) => {
const hasCallback = typeof args[args.length - 1] === `function`;
const callback = hasCallback ? args.pop() : () => {};
process.nextTick(() => {
fakeImpl.apply(fakeFs, args).then((result: any) => {
callback(null, result);
}, (error: Error) => {
callback(error);
});
});
};
setupFn(patchedFs, origName, wrapper);
}
patchedFs.realpath.native = patchedFs.realpath;
}
/** Sync implementations */
{
setupFn(patchedFs, `existsSync`, (p: string) => {
try {
return fakeFs.existsSync(p);
} catch (error) {
return false;
}
});
for (const fnName of SYNC_IMPLEMENTATIONS) {
const origName = fnName;
if (typeof (patchedFs as any)[origName] === `undefined`)
continue;
const fakeImpl: Function = (fakeFs as any)[fnName];
if (typeof fakeImpl === `undefined`)
continue;
setupFn(patchedFs, origName, fakeImpl.bind(fakeFs));
}
patchedFs.realpathSync.native = patchedFs.realpathSync;
}
/** Promise implementations */
{
// `fs.promises` is a getter that returns a reference to require(`fs/promises`),
// so we can just patch `fs.promises` and both will be updated
const origEmitWarning = process.emitWarning;
process.emitWarning = () => {};
let patchedFsPromises;
try {
patchedFsPromises = patchedFs.promises;
} finally {
process.emitWarning = origEmitWarning;
}
if (typeof patchedFsPromises !== `undefined`) {
// `fs.promises.exists` doesn't exist
for (const fnName of ASYNC_IMPLEMENTATIONS) {
const origName = fnName.replace(/Promise$/, ``);
if (typeof (patchedFsPromises as any)[origName] === `undefined`)
continue;
const fakeImpl: Function = (fakeFs as any)[fnName];
if (typeof fakeImpl === `undefined`)
continue;
// Open is a bit particular with fs.promises: it returns a file handle
// instance instead of the traditional file descriptor number
if (fnName === `open`)
continue;
setupFn(patchedFsPromises, origName, fakeImpl.bind(fakeFs));
}
class FileHandle {
constructor(public fd: number) {
}
}
for (const fnName of FILEHANDLE_IMPLEMENTATIONS) {
const origName = fnName.replace(/Promise$/, ``);
const fakeImpl: Function = (fakeFs as any)[fnName];
if (typeof fakeImpl === `undefined`)
continue;
setupFn(FileHandle.prototype, origName, function (this: FileHandle, ...args: Array<any>) {
return fakeImpl.call(fakeFs, this.fd, ...args);
});
}
setupFn(patchedFsPromises, `open`, async (...args: Array<any>) => {
// @ts-expect-error
const fd = await fakeFs.openPromise(...args);
return new FileHandle(fd);
});
// `fs.promises.realpath` doesn't have a `native` property
}
}
/** util.promisify implementations */
{
// Override the promisified version of `fs.read` to return an object as per
// https://github.com/nodejs/node/blob/dc79f3f37caf6f25b8efee4623bec31e2c20f595/lib/fs.js#L559-L560
// and
// https://github.com/nodejs/node/blob/ba684805b6c0eded76e5cd89ee00328ac7a59365/lib/internal/util.js#L293
// @ts-expect-error
patchedFs.read[promisify.custom] = async (p: number, buffer: Buffer, ...args: Array<any>) => {
const res = fakeFs.readPromise(p, buffer, ...args);
return {bytesRead: await res, buffer};
};
}
}
export function extendFs(realFs: typeof fs, fakeFs: FakeFS<NativePath>): typeof fs {
const patchedFs = Object.create(realFs);
patchFs(patchedFs, fakeFs);
return patchedFs;
}