Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions __test__/read_file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ test('readFile: async should throw on non-existent file', async (t) => {
await t.throwsAsync(async () => await readFile('./no-such-file'), { message: /ENOENT/ })
})

test('readFile: async should return string with encoding as string param', async (t) => {
const result = await readFile('./package.json', 'utf-8')
t.is(typeof result, 'string')
})

test('readFile: async should return string with encoding as options object', async (t) => {
const result = await readFile('./package.json', { encoding: 'utf-8' })
t.is(typeof result, 'string')
})

test('readFile: async should return Buffer with no encoding', async (t) => {
const result = await readFile('./package.json')
t.true(Buffer.isBuffer(result))
})

test('dual-run: readFileSync Buffer should match node:fs byte-for-byte', (t) => {
const nodeResult = nodeFs.readFileSync('./package.json')
const hyperResult = readFileSync('./package.json') as Buffer
Expand Down
32 changes: 24 additions & 8 deletions src/read_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,28 @@ pub struct ReadFileOptions {
pub flag: Option<String>,
}

fn normalize_read_file_options(
options: Option<Either<String, ReadFileOptions>>,
) -> ReadFileOptions {
match options {
Some(Either::A(encoding)) => ReadFileOptions {
encoding: Some(encoding),
flag: None,
},
Some(Either::B(opts)) => opts,
None => ReadFileOptions {
encoding: None,
flag: None,
},
}
}

fn read_file_impl(
path_str: String,
options: Option<ReadFileOptions>,
options: Option<Either<String, ReadFileOptions>>,
) -> Result<Either<String, Buffer>> {
let path = Path::new(&path_str);
let opts = options.unwrap_or(ReadFileOptions {
encoding: None,
flag: None,
});
let opts = normalize_read_file_options(options);

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

Expand Down Expand Up @@ -142,7 +155,7 @@ fn read_file_impl(
#[napi(js_name = "readFileSync")]
pub fn read_file_sync(
path: String,
options: Option<ReadFileOptions>,
options: Option<Either<String, ReadFileOptions>>,
) -> Result<Either<String, Buffer>> {
read_file_impl(path, options)
}
Expand All @@ -151,7 +164,7 @@ pub fn read_file_sync(

pub struct ReadFileTask {
pub path: String,
pub options: Option<ReadFileOptions>,
pub options: Option<Either<String, ReadFileOptions>>,
}

impl Task for ReadFileTask {
Expand All @@ -168,6 +181,9 @@ impl Task for ReadFileTask {
}

#[napi(js_name = "readFile")]
pub fn read_file(path: String, options: Option<ReadFileOptions>) -> AsyncTask<ReadFileTask> {
pub fn read_file(
path: String,
options: Option<Either<String, ReadFileOptions>>,
) -> AsyncTask<ReadFileTask> {
AsyncTask::new(ReadFileTask { path, options })
}