@@ -2,19 +2,36 @@ import { promises as fs, readFileSync as fsReadFileSync } from 'node:fs'
2
2
import path from 'node:path'
3
3
import process from 'node:process'
4
4
5
+ import constants from '../constants'
6
+
7
+ import type { Remap } from '@socketsecurity/registry/lib/objects'
5
8
import type { Abortable } from 'node:events'
6
- import type { ObjectEncodingOptions , OpenMode , PathLike } from 'node:fs'
9
+ import type {
10
+ ObjectEncodingOptions ,
11
+ OpenMode ,
12
+ PathLike ,
13
+ PathOrFileDescriptor
14
+ } from 'node:fs'
7
15
import type { FileHandle } from 'node:fs/promises'
8
16
17
+ const { abortSignal } = constants
18
+
19
+ export type FindUpOptions = {
20
+ cwd ?: string | undefined
21
+ signal ?: AbortSignal | undefined
22
+ }
9
23
export async function findUp (
10
24
name : string | string [ ] ,
11
- { cwd = process . cwd ( ) } : { cwd : string }
25
+ { cwd = process . cwd ( ) , signal = abortSignal } : FindUpOptions
12
26
) : Promise < string | undefined > {
13
27
let dir = path . resolve ( cwd )
14
28
const { root } = path . parse ( dir )
15
29
const names = [ name ] . flat ( )
16
30
while ( dir && dir !== root ) {
17
31
for ( const name of names ) {
32
+ if ( signal ?. aborted ) {
33
+ return undefined
34
+ }
18
35
const filePath = path . join ( dir , name )
19
36
try {
20
37
// eslint-disable-next-line no-await-in-loop
@@ -29,16 +46,19 @@ export async function findUp(
29
46
return undefined
30
47
}
31
48
32
- export type ReadFileOptions = ObjectEncodingOptions &
33
- Abortable & {
34
- flag ?: OpenMode | undefined
35
- }
49
+ export type ReadFileOptions = Remap <
50
+ ObjectEncodingOptions &
51
+ Abortable & {
52
+ flag ?: OpenMode | undefined
53
+ }
54
+ >
36
55
37
56
export async function readFileBinary (
38
57
filepath : PathLike | FileHandle ,
39
58
options ?: ReadFileOptions | undefined
40
59
) : Promise < Buffer > {
41
60
return ( await fs . readFile ( filepath , {
61
+ signal : abortSignal ,
42
62
...options ,
43
63
encoding : 'binary'
44
64
} as ReadFileOptions ) ) as Buffer
@@ -48,26 +68,50 @@ export async function readFileUtf8(
48
68
filepath : PathLike | FileHandle ,
49
69
options ?: ReadFileOptions | undefined
50
70
) : Promise < string > {
51
- return ( await fs . readFile ( filepath , {
71
+ return await fs . readFile ( filepath , {
72
+ signal : abortSignal ,
52
73
...options ,
53
74
encoding : 'utf8'
54
- } as ReadFileOptions ) ) as string
75
+ } )
55
76
}
56
77
57
78
export async function safeReadFile (
58
- ...args : Parameters < typeof fs . readFile >
59
- ) : Promise < ReturnType < typeof fs . readFile > | undefined > {
79
+ filepath : PathLike | FileHandle ,
80
+ options ?: 'utf8' | 'utf-8' | { encoding : 'utf8' | 'utf-8' } | undefined
81
+ ) : Promise < string | undefined >
82
+ export async function safeReadFile (
83
+ filepath : PathLike | FileHandle ,
84
+ options ?: ReadFileOptions | NodeJS . BufferEncoding | undefined
85
+ ) : Promise < Awaited < ReturnType < typeof fs . readFile > > | undefined > {
60
86
try {
61
- return await fs . readFile ( ...args )
87
+ return await fs . readFile ( filepath , {
88
+ encoding : 'utf8' ,
89
+ signal : abortSignal ,
90
+ ...( typeof options === 'string' ? { encoding : options } : options )
91
+ } )
62
92
} catch { }
63
93
return undefined
64
94
}
65
95
66
96
export function safeReadFileSync (
67
- ...args : Parameters < typeof fsReadFileSync >
97
+ filepath : PathOrFileDescriptor ,
98
+ options ?: 'utf8' | 'utf-8' | { encoding : 'utf8' | 'utf-8' } | undefined
99
+ ) : string | undefined
100
+ export function safeReadFileSync (
101
+ filepath : PathOrFileDescriptor ,
102
+ options ?:
103
+ | {
104
+ encoding ?: NodeJS . BufferEncoding | undefined
105
+ flag ?: string | undefined
106
+ }
107
+ | NodeJS . BufferEncoding
108
+ | undefined
68
109
) : ReturnType < typeof fsReadFileSync > | undefined {
69
110
try {
70
- return fsReadFileSync ( ...args )
111
+ return fsReadFileSync ( filepath , {
112
+ encoding : 'utf8' ,
113
+ ...( typeof options === 'string' ? { encoding : options } : options )
114
+ } )
71
115
} catch { }
72
116
return undefined
73
117
}
0 commit comments