Skip to content

Commit 2c87ac1

Browse files
committed
refactor: improve fs typings
1 parent ef84902 commit 2c87ac1

8 files changed

Lines changed: 41 additions & 38 deletions

js/private/node-patches/fs.cjs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ function patcher(roots) {
351351
const cb = once(args[args.length - 1]);
352352
args[args.length - 1] = async function opendirCb(err, dir) {
353353
try {
354-
cb(null, handleDir(dir));
354+
cb(err, err ? undefined : handleDir(dir));
355355
}
356356
catch (err) {
357357
cb(err);
@@ -452,16 +452,18 @@ function patcher(roots) {
452452
});
453453
};
454454
const origRead = dir.read.bind(dir);
455-
dir.read = async function handleDirRead(...args) {
456-
if (typeof args[args.length - 1] === 'function') {
457-
const cb = args[args.length - 1];
458-
args[args.length - 1] = async function handleDirReadCb(err, entry) {
459-
cb(err, entry ? await handleDirent(p, entry) : null);
460-
};
461-
origRead(...args);
455+
dir.read = async function handleDirRead(cb) {
456+
if (typeof cb === 'function') {
457+
origRead(function handleDirReadCb(err, entry) {
458+
if (err)
459+
return cb(err, null);
460+
handleDirent(p, entry).then(() => {
461+
cb(null, entry);
462+
}, (err) => cb(err, null));
463+
});
462464
}
463465
else {
464-
const entry = await origRead(...args);
466+
const entry = await origRead();
465467
if (entry) {
466468
await handleDirent(p, entry);
467469
}

js/private/node-patches/src/fs.cts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import * as path from 'path'
2222
import * as util from 'util'
2323

2424
// windows cant find the right types
25-
type Dir = any
26-
type Dirent = any
25+
type Dir = FsType.Dir
26+
type Dirent = FsType.Dirent
2727

2828
// using require here on purpose so we can override methods with any
2929
// also even though imports are mutable in typescript the cognitive dissonance is too high because
@@ -111,7 +111,7 @@ export function patcher(roots: string[]): () => void {
111111
return origLstat(...args)
112112
}
113113

114-
const cb = once(args[args.length - 1] as any)
114+
const cb = once(args[args.length - 1] as Function)
115115

116116
// override the callback
117117
args[args.length - 1] = function lstatCb(err: Error, stats: Stats) {
@@ -207,7 +207,7 @@ export function patcher(roots: string[]): () => void {
207207
return origRealpath(...args)
208208
}
209209

210-
const cb = once(args[args.length - 1] as any)
210+
const cb = once(args[args.length - 1] as Function)
211211

212212
args[args.length - 1] = function realpathCb(err: Error, str: string) {
213213
if (err) return cb(err)
@@ -230,7 +230,7 @@ export function patcher(roots: string[]): () => void {
230230
return origRealpathNative(...args)
231231
}
232232

233-
const cb = once(args[args.length - 1] as any)
233+
const cb = once(args[args.length - 1] as Function)
234234

235235
args[args.length - 1] = function nativeCb(err: Error, str: string) {
236236
if (err) return cb(err)
@@ -277,7 +277,7 @@ export function patcher(roots: string[]): () => void {
277277
return origReadlink(...args)
278278
}
279279

280-
const cb = once(args[args.length - 1] as any)
280+
const cb = once(args[args.length - 1] as Function)
281281

282282
args[args.length - 1] = function readlinkCb(err: Error, str: string) {
283283
if (err) return cb(err)
@@ -363,7 +363,7 @@ export function patcher(roots: string[]): () => void {
363363
return origReaddir(...args)
364364
}
365365

366-
const cb = once(args[args.length - 1] as any)
366+
const cb = once(args[args.length - 1] as Function)
367367
const p = resolvePathLike(args[0])
368368

369369
args[args.length - 1] = function readdirCb(
@@ -410,13 +410,13 @@ export function patcher(roots: string[]): () => void {
410410
// if this is not a function opendir should throw an error.
411411
// we call it so we don't have to throw a mock
412412
if (typeof args[args.length - 1] === 'function') {
413-
const cb = once(args[args.length - 1] as any)
413+
const cb = once(args[args.length - 1] as Function)
414414
args[args.length - 1] = async function opendirCb(
415415
err: Error,
416416
dir: Dir
417417
) {
418418
try {
419-
cb(null, handleDir(dir))
419+
cb(err, err ? undefined : handleDir(dir))
420420
} catch (err) {
421421
cb(err)
422422
}
@@ -460,7 +460,7 @@ export function patcher(roots: string[]): () => void {
460460
let unpatchPromises: Function
461461

462462
if (promisePropertyDescriptor) {
463-
const promises: any = {}
463+
const promises: typeof fs.promises = {}
464464
promises.lstat = util.promisify(fs.lstat)
465465
// NOTE: node core uses the newer realpath function fs.promises.native instead of fs.realPath
466466
promises.realpath = util.promisify(fs.realpath.native)
@@ -523,19 +523,20 @@ export function patcher(roots: string[]): () => void {
523523

524524
const origRead = dir.read.bind(dir)
525525
dir.read = async function handleDirRead(
526-
...args: Parameters<typeof dir.read>
526+
cb?: Parameters<typeof dir.read>[0]
527527
) {
528-
if (typeof args[args.length - 1] === 'function') {
529-
const cb = args[args.length - 1] as Function
530-
args[args.length - 1] = async function handleDirReadCb(
531-
err: Error,
532-
entry: Dirent
533-
) {
534-
cb(err, entry ? await handleDirent(p, entry) : null)
535-
}
536-
origRead(...args)
528+
if (typeof cb === 'function') {
529+
origRead(function handleDirReadCb(err: Error, entry: Dirent) {
530+
if (err) return cb(err, null)
531+
handleDirent(p, entry).then(
532+
() => {
533+
cb(null, entry)
534+
},
535+
(err) => cb(err, null)
536+
)
537+
})
537538
} else {
538-
const entry = await origRead(...args)
539+
const entry = await origRead()
539540
if (entry) {
540541
await handleDirent(p, entry)
541542
}
@@ -975,10 +976,10 @@ export function escapeFunction(_roots: string[]) {
975976
}
976977
}
977978

978-
function once<T>(fn: (...args: unknown[]) => T) {
979+
function once(fn: Function) {
979980
let called = false
980981

981-
return function callOnce(...args: unknown[]) {
982+
return function callOnce(...args: any[]) {
982983
if (called) return
983984
called = true
984985

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
98cf9ac065dc5dc637ea6e803c5458c39a0310503e64c0eb742d65cb05407d31 js/private/test/image/cksum_node
1+
979f06636582a6d515442b13bb048db0b66ed2febfa458a02d8745b691e9b2ff js/private/test/image/cksum_node
22
52836a988c8ac815b4a3b70fa3a3acec67b851699fa989694cef4cc1fa53de96 js/private/test/image/cksum_package_store_3p
33
642b308a0561fb51dfd96d08d74a4ec419c9d2ca501cfa1002a49c8e25fbe4c2 js/private/test/image/cksum_package_store_1p
44
5d45f32dacf0b83e26c33d4e1017c694e79eaff29b8ecc336f9ea8fdee870d45 js/private/test/image/cksum_node_modules

js/private/test/image/custom_layers_nomatch_test_node.listing

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.
88
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/
99
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/
1010
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/
11-
-r-xr-xr-x 0 0 0 34629 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
11+
-r-xr-xr-x 0 0 0 34656 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
1212
-r-xr-xr-x 0 0 0 1460 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
1313
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
1414
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/

js/private/test/image/custom_owner_test_node.listing

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runf
77
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/
88
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/
99
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/
10-
-r-xr-xr-x 0 100 0 34629 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
10+
-r-xr-xr-x 0 100 0 34656 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
1111
-r-xr-xr-x 0 100 0 1460 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
1212
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
1313
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/

js/private/test/image/default_test_node.listing

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runf
77
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/
88
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/
99
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/
10-
-r-xr-xr-x 0 0 0 34629 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
10+
-r-xr-xr-x 0 0 0 34656 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
1111
-r-xr-xr-x 0 0 0 1460 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
1212
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
1313
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/

js/private/test/image/non_ascii/custom_layer_groups_test_just_the_fs_patch.listing

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_
99
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/
1010
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/
1111
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/node-patches/
12-
-r-xr-xr-x 0 0 0 34629 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/node-patches/fs.cjs
12+
-r-xr-xr-x 0 0 0 34656 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/node-patches/fs.cjs

js/private/test/image/regex_edge_cases_test_node.listing

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.
88
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/
99
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/
1010
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/
11-
-r-xr-xr-x 0 0 0 34629 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
11+
-r-xr-xr-x 0 0 0 34656 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
1212
-r-xr-xr-x 0 0 0 1460 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
1313
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
1414
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/

0 commit comments

Comments
 (0)