Skip to content

Commit c98494b

Browse files
robhoganmeta-codesync[bot]
authored andcommitted
Add Flow lib defs for newer Node.js fs APIs (#54992)
Summary: Pull Request resolved: #54992 Node.js's `fs` has been updated considerably since it was first covered with Flow, and so far we've only added new APIs piecemeal as we need them. This goes through the whole module top to bottom and attempts to update in line with the Node.js docs at v24. Changelog: [Internal] Reviewed By: vzaidman Differential Revision: D89605689 fbshipit-source-id: 1ad0ef2f7fedb6416471461b8be7bc418afcd28f
1 parent 61a838d commit c98494b

File tree

1 file changed

+224
-16
lines changed

1 file changed

+224
-16
lines changed

flow-typed/environment/node.js

Lines changed: 224 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,8 @@ declare module 'fs' {
10501050

10511051
declare class FSWatcher extends events$EventEmitter {
10521052
close(): void;
1053+
ref(): this;
1054+
unref(): this;
10531055
}
10541056

10551057
declare class ReadStream extends stream$Readable {
@@ -1063,6 +1065,7 @@ declare module 'fs' {
10631065

10641066
declare class Dirent {
10651067
name: string | Buffer;
1068+
parentPath: string;
10661069

10671070
isBlockDevice(): boolean;
10681071
isCharacterDevice(): boolean;
@@ -1296,12 +1299,24 @@ declare module 'fs' {
12961299
declare function mkdtempSync(prefix: string): string;
12971300
declare function readdir(
12981301
path: string,
1299-
options: string | {encoding?: string, withFileTypes?: false, ...},
1302+
options:
1303+
| string
1304+
| $ReadOnly<{
1305+
encoding?: string,
1306+
recursive?: boolean,
1307+
withFileTypes?: false,
1308+
...
1309+
}>,
13001310
callback: (err: ?ErrnoError, files: Array<string>) => void,
13011311
): void;
13021312
declare function readdir(
13031313
path: string,
1304-
options: {encoding?: string, withFileTypes: true, ...},
1314+
options: $ReadOnly<{
1315+
encoding?: string,
1316+
recursive?: boolean,
1317+
withFileTypes: true,
1318+
...
1319+
}>,
13051320
callback: (err: ?ErrnoError, files: Array<Dirent>) => void,
13061321
): void;
13071322
declare function readdir(
@@ -1310,11 +1325,23 @@ declare module 'fs' {
13101325
): void;
13111326
declare function readdirSync(
13121327
path: string,
1313-
options?: string | {encoding?: string, withFileTypes?: false, ...},
1328+
options?:
1329+
| string
1330+
| $ReadOnly<{
1331+
encoding?: string,
1332+
recursive?: boolean,
1333+
withFileTypes?: false,
1334+
}>,
13141335
): Array<string>;
13151336
declare function readdirSync(
13161337
path: string,
1317-
options?: string | {encoding?: string, withFileTypes: true, ...},
1338+
options?:
1339+
| string
1340+
| $ReadOnly<{
1341+
encoding?: string,
1342+
recursive?: boolean,
1343+
withFileTypes: true,
1344+
}>,
13181345
): Array<Dirent>;
13191346
declare function close(
13201347
fd: number,
@@ -1332,6 +1359,29 @@ declare module 'fs' {
13321359
flags: string | number,
13331360
callback: (err: ?ErrnoError, fd: number) => void,
13341361
): void;
1362+
declare function openAsBlob(
1363+
path: string | Buffer | URL,
1364+
options?: $ReadOnly<{
1365+
type?: string, // Optional MIME type hint
1366+
}>,
1367+
): Promise<Blob>;
1368+
declare function opendir(
1369+
path: string,
1370+
options?: $ReadOnly<{
1371+
encoding?: string,
1372+
bufferSize?: number,
1373+
recursive?: boolean,
1374+
}>,
1375+
callback: (err: ?ErrnoError, dir: Dir) => void,
1376+
): void;
1377+
declare function opendirSync(
1378+
path: string,
1379+
options?: $ReadOnly<{
1380+
encoding?: string,
1381+
bufferSize?: number,
1382+
recursive?: boolean,
1383+
}>,
1384+
): Dir;
13351385
declare function openSync(
13361386
path: string | Buffer,
13371387
flags: string | number,
@@ -1550,7 +1600,15 @@ declare module 'fs' {
15501600
): void;
15511601
declare function watchFile(
15521602
filename: string,
1553-
options?: Object,
1603+
listener?: (curr: Stats, prev: Stats) => void,
1604+
): void;
1605+
declare function watchFile(
1606+
filename: string,
1607+
options?: $ReadOnly<{
1608+
bigint?: boolean,
1609+
persistent?: boolean,
1610+
interval?: number,
1611+
}>,
15541612
listener?: (curr: Stats, prev: Stats) => void,
15551613
): void;
15561614
declare function unwatchFile(
@@ -1559,7 +1617,16 @@ declare module 'fs' {
15591617
): void;
15601618
declare function watch(
15611619
filename: string,
1562-
options?: Object,
1620+
listener?: (event: string, filename: string) => void,
1621+
): FSWatcher;
1622+
declare function watch(
1623+
filename: string,
1624+
options?: $ReadOnly<{
1625+
persistent?: boolean,
1626+
recursive?: boolean,
1627+
encoding?: string,
1628+
signal?: AbortSignal,
1629+
}>,
15631630
listener?: (event: string, filename: string) => void,
15641631
): FSWatcher;
15651632
declare function exists(
@@ -1599,6 +1666,40 @@ declare module 'fs' {
15991666
dest: string,
16001667
flags?: number,
16011668
): void;
1669+
declare function cp(
1670+
src: string | URL,
1671+
dest: string | URL,
1672+
options: $ReadOnly<{
1673+
dereference?: boolean,
1674+
errorOnExist?: boolean,
1675+
filter?: (src: string, dest: string) => boolean | Promise<boolean>,
1676+
force?: boolean,
1677+
mode?: number,
1678+
preserveTimestamps?: boolean,
1679+
recursive?: boolean,
1680+
verbatimSymlinks?: boolean,
1681+
}>,
1682+
callback: (err: ?Error) => void,
1683+
): void;
1684+
declare function cp(
1685+
src: string | URL,
1686+
dest: string | URL,
1687+
callback: (err: ?Error) => void,
1688+
): void;
1689+
declare function cpSync(
1690+
src: string | URL,
1691+
dest: string | URL,
1692+
options?: $ReadOnly<{
1693+
dereference?: boolean,
1694+
errorOnExist?: boolean,
1695+
filter?: (src: string, dest: string) => boolean,
1696+
force?: boolean,
1697+
mode?: number,
1698+
preserveTimestamps?: boolean,
1699+
recursive?: boolean,
1700+
verbatimSymlinks?: boolean,
1701+
}>,
1702+
): void;
16021703

16031704
declare type GlobOptions<WithFileTypes: boolean> = $ReadOnly<{
16041705
/**
@@ -1729,14 +1830,52 @@ declare module 'fs' {
17291830
retryDelay?: number,
17301831
...
17311832
};
1833+
declare class Dir {
1834+
+path: string;
1835+
close(): Promise<void>;
1836+
closeSync(): void;
1837+
read(): Promise<?Dirent>;
1838+
read(cb: (err?: Error, dirent: ?Dirent) => void): void;
1839+
readSync(): ?Dirent;
1840+
@@asyncIterator(): AsyncIterator<Dirent>;
1841+
}
1842+
type AppendOrWriteToFileHandle = (
1843+
data:
1844+
| string
1845+
| Buffer
1846+
| Uint8Array
1847+
| DataView
1848+
| AsyncIterable<mixed>
1849+
| Iterable<mixed>
1850+
| stream$Readable,
1851+
options: WriteOptions | string,
1852+
) => Promise<void>;
17321853
declare class FileHandle {
1733-
appendFile(
1734-
data: string | Buffer,
1735-
options: WriteOptions | string,
1736-
): Promise<void>;
1854+
appendFile: AppendOrWriteToFileHandle;
17371855
chmod(mode: number): Promise<void>;
17381856
chown(uid: number, guid: number): Promise<void>;
17391857
close(): Promise<void>;
1858+
createReadStream(
1859+
options?: $ReadOnly<{
1860+
encoding?: string,
1861+
autoClose?: boolean,
1862+
emitClose?: boolean,
1863+
start?: number,
1864+
end?: number,
1865+
highWaterMark?: number,
1866+
signal?: AbortSignal,
1867+
}>,
1868+
): ReadStream;
1869+
createWriteStream(
1870+
options?: $ReadOnly<{
1871+
encoding?: string,
1872+
autoClose?: boolean,
1873+
emitClose?: boolean,
1874+
start?: number,
1875+
highWaterMark?: number,
1876+
flush?: boolean,
1877+
}>,
1878+
): WriteStream;
17401879
datasync(): Promise<void>;
17411880
fd: number;
17421881
read<T: Buffer | Uint8Array>(
@@ -1749,8 +1888,25 @@ declare module 'fs' {
17491888
buffer: T,
17501889
...
17511890
}>;
1891+
readableWebStream(
1892+
options?: $ReadOnly<{autoClose?: boolean}>,
1893+
): ReadableStream;
17521894
readFile(options: EncodingFlag): Promise<Buffer>;
17531895
readFile(options: string): Promise<string>;
1896+
readLines(
1897+
options?: $ReadOnly<{
1898+
encoding?: string,
1899+
autoClose?: boolean,
1900+
emitClose?: boolean,
1901+
start?: number,
1902+
end?: number,
1903+
highWaterMark?: number,
1904+
}>,
1905+
): readline$Interface;
1906+
readv<T: Array<Buffer> | Array<Uint8Array> | Array<DataView>>(
1907+
buffers: T,
1908+
position?: number | null,
1909+
): Promise<{buffers: T, bytesRead: number}>;
17541910
stat(): Promise<Stats>;
17551911
sync(): Promise<void>;
17561912
truncate(len?: number): Promise<void>;
@@ -1759,15 +1915,24 @@ declare module 'fs' {
17591915
mtime: number | string | Date,
17601916
): Promise<void>;
17611917
write(
1762-
buffer: Buffer | Uint8Array,
1918+
buffer: Buffer | Uint8Array | DataView,
17631919
offset: number,
17641920
length: number,
17651921
position: number,
17661922
): Promise<void>;
1767-
writeFile(
1768-
data: string | Buffer | Uint8Array,
1769-
options: WriteOptions | string,
1923+
write(
1924+
buffer: Buffer | Uint8Array | DataView,
1925+
options?: $ReadOnly<{
1926+
offset?: number,
1927+
length?: number,
1928+
position?: number,
1929+
}>,
17701930
): Promise<void>;
1931+
writeFile: AppendOrWriteToFileHandle;
1932+
writev<T: Array<Buffer> | Array<Uint8Array> | Array<DataView>>(
1933+
buffers: T,
1934+
position?: number | null,
1935+
): Promise<{buffers: T, bytesWritten: number}>;
17711936
}
17721937

17731938
declare type FSPromisePath = string | Buffer | URL;
@@ -1785,6 +1950,20 @@ declare module 'fs' {
17851950
dest: FSPromisePath,
17861951
flags?: number,
17871952
): Promise<void>,
1953+
cp(
1954+
src: string | URL,
1955+
dest: string | URL,
1956+
options?: $ReadOnly<{
1957+
dereference?: boolean,
1958+
errorOnExist?: boolean,
1959+
filter?: (src: string, dest: string) => boolean | Promise<boolean>,
1960+
force?: boolean,
1961+
mode?: number,
1962+
preserveTimestamps?: boolean,
1963+
recursive?: boolean,
1964+
verbatimSymlinks?: boolean,
1965+
}>,
1966+
): Promise<void>,
17881967
fchmod(filehandle: FileHandle, mode: number): Promise<void>,
17891968
fchown(filehandle: FileHandle, uid: number, guid: number): Promise<void>,
17901969
fdatasync(filehandle: FileHandle): Promise<void>,
@@ -1824,6 +2003,14 @@ declare module 'fs' {
18242003
flags?: string | number,
18252004
mode?: number,
18262005
): Promise<FileHandle>,
2006+
opendir(
2007+
path: string,
2008+
options?: $ReadOnly<{
2009+
encoding?: string,
2010+
bufferSize?: number,
2011+
recursive?: boolean,
2012+
}>,
2013+
): Promise<Dir>,
18272014
read<T: Buffer | Uint8Array>(
18282015
filehandle: FileHandle,
18292016
buffer: T,
@@ -1837,11 +2024,21 @@ declare module 'fs' {
18372024
}>,
18382025
readdir: ((
18392026
path: FSPromisePath,
1840-
options: string | {encoding?: string, withFileTypes?: false, ...},
2027+
options:
2028+
| string
2029+
| $ReadOnly<{
2030+
encoding?: string,
2031+
recursive?: boolean,
2032+
withFileTypes?: false,
2033+
}>,
18412034
) => Promise<Array<string>>) &
18422035
((
18432036
path: FSPromisePath,
1844-
options: {encoding?: string, withFileTypes: true, ...},
2037+
options: $ReadOnly<{
2038+
encoding?: string,
2039+
recursive?: boolean,
2040+
withFileTypes: true,
2041+
}>,
18452042
) => Promise<Array<Dirent>>) &
18462043
((path: FSPromisePath) => Promise<Array<string>>),
18472044
readFile: ((
@@ -1884,6 +2081,17 @@ declare module 'fs' {
18842081
atime: number | string | Date,
18852082
mtime: number | string | Date,
18862083
): Promise<void>,
2084+
watch(
2085+
filename: FSPromisePath,
2086+
options?: $ReadOnly<{
2087+
persistent?: boolean,
2088+
recursive?: boolean,
2089+
encoding?: string,
2090+
signal?: AbortSignal,
2091+
maxQueue?: number,
2092+
overflow?: 'ignore' | 'throw',
2093+
}>,
2094+
): AsyncIterator<{eventType: string, filename: ?string}>,
18872095
write<T: Buffer | Uint8Array>(
18882096
filehandle: FileHandle,
18892097
buffer: T,

0 commit comments

Comments
 (0)