Skip to content

Commit dbf95d4

Browse files
authored
Merge pull request #17 from CoderSerio/fix/readfile-encoding-param
fix: support string encoding parameter in readFile
2 parents 2681195 + 0545c8f commit dbf95d4

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

__test__/read_file.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ test('readFile: async should throw on non-existent file', async (t) => {
3636
await t.throwsAsync(async () => await readFile('./no-such-file'), { message: /ENOENT/ })
3737
})
3838

39+
test('readFile: async should return string with encoding as string param', async (t) => {
40+
const result = await readFile('./package.json', 'utf-8')
41+
t.is(typeof result, 'string')
42+
})
43+
44+
test('readFile: async should return string with encoding as options object', async (t) => {
45+
const result = await readFile('./package.json', { encoding: 'utf-8' })
46+
t.is(typeof result, 'string')
47+
})
48+
49+
test('readFile: async should return Buffer with no encoding', async (t) => {
50+
const result = await readFile('./package.json')
51+
t.true(Buffer.isBuffer(result))
52+
})
53+
3954
test('dual-run: readFileSync Buffer should match node:fs byte-for-byte', (t) => {
4055
const nodeResult = nodeFs.readFileSync('./package.json')
4156
const hyperResult = readFileSync('./package.json') as Buffer

src/read_file.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,28 @@ pub struct ReadFileOptions {
6666
pub flag: Option<String>,
6767
}
6868

69+
fn normalize_read_file_options(
70+
options: Option<Either<String, ReadFileOptions>>,
71+
) -> ReadFileOptions {
72+
match options {
73+
Some(Either::A(encoding)) => ReadFileOptions {
74+
encoding: Some(encoding),
75+
flag: None,
76+
},
77+
Some(Either::B(opts)) => opts,
78+
None => ReadFileOptions {
79+
encoding: None,
80+
flag: None,
81+
},
82+
}
83+
}
84+
6985
fn read_file_impl(
7086
path_str: String,
71-
options: Option<ReadFileOptions>,
87+
options: Option<Either<String, ReadFileOptions>>,
7288
) -> Result<Either<String, Buffer>> {
7389
let path = Path::new(&path_str);
74-
let opts = options.unwrap_or(ReadFileOptions {
75-
encoding: None,
76-
flag: None,
77-
});
90+
let opts = normalize_read_file_options(options);
7891

7992
let flag = opts.flag.as_deref().unwrap_or("r");
8093

@@ -142,7 +155,7 @@ fn read_file_impl(
142155
#[napi(js_name = "readFileSync")]
143156
pub fn read_file_sync(
144157
path: String,
145-
options: Option<ReadFileOptions>,
158+
options: Option<Either<String, ReadFileOptions>>,
146159
) -> Result<Either<String, Buffer>> {
147160
read_file_impl(path, options)
148161
}
@@ -151,7 +164,7 @@ pub fn read_file_sync(
151164

152165
pub struct ReadFileTask {
153166
pub path: String,
154-
pub options: Option<ReadFileOptions>,
167+
pub options: Option<Either<String, ReadFileOptions>>,
155168
}
156169

157170
impl Task for ReadFileTask {
@@ -168,6 +181,9 @@ impl Task for ReadFileTask {
168181
}
169182

170183
#[napi(js_name = "readFile")]
171-
pub fn read_file(path: String, options: Option<ReadFileOptions>) -> AsyncTask<ReadFileTask> {
184+
pub fn read_file(
185+
path: String,
186+
options: Option<Either<String, ReadFileOptions>>,
187+
) -> AsyncTask<ReadFileTask> {
172188
AsyncTask::new(ReadFileTask { path, options })
173189
}

0 commit comments

Comments
 (0)