forked from nodelib/nodelib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.ts
37 lines (27 loc) · 1.16 KB
/
fs.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
import * as fs from 'node:fs';
import type { Dirent } from '../types';
type DirentStatsKeysIntersection = keyof fs.Dirent & keyof fs.Stats;
const kStats = Symbol('stats');
export function createDirentFromStats(name: string, stats: fs.Stats): Dirent {
return new DirentFromStats(name, stats);
}
// Adapting an internal class in Node.js to mimic the behavior of `fs.Dirent` when creating it manually from `fs.Stats`.
// https://github.com/nodejs/node/blob/a4cf6b204f0b160480153dc293ae748bf15225f9/lib/internal/fs/utils.js#L199C1-L213
export class DirentFromStats extends fs.Dirent {
private readonly [kStats]: fs.Stats;
constructor(name: string, stats: fs.Stats) {
// @ts-expect-error The constructor has parameters, but they are not represented in types.
// https://github.com/nodejs/node/blob/a4cf6b204f0b160480153dc293ae748bf15225f9/lib/internal/fs/utils.js#L164
super(name, null);
this[kStats] = stats;
}
}
for (const key of Reflect.ownKeys(fs.Dirent.prototype)) {
const name = key as DirentStatsKeysIntersection | 'constructor';
if (name === 'constructor') {
continue;
}
DirentFromStats.prototype[name] = function () {
return this[kStats][name]();
};
}