Skip to content

Commit d23bac5

Browse files
committed
fix: fix issue awslabs#123
1 parent 4b1afe6 commit d23bac5

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

src/fs/access.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::utils::result::ResultExt;
1111
use super::{CONSTANT_F_OK, CONSTANT_R_OK, CONSTANT_W_OK, CONSTANT_X_OK};
1212

1313
pub async fn access(ctx: Ctx<'_>, path: String, mode: Opt<u32>) -> Result<()> {
14-
let metadata = fs::metadata(path.clone())
14+
let metadata = fs::metadata(&path)
1515
.await
1616
.or_throw_msg(&ctx, &format!("Can't access file \"{}\"", &path))?;
1717

src/fs/read_file.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
use rquickjs::{function::Opt, Ctx, Object, Result};
3+
use rquickjs::{function::Opt, Ctx, Exception, IntoJs, Object, Result, String as JsString, Value};
44
use tokio::fs;
55

66
use crate::{buffer::Buffer, utils::result::ResultExt};
77

8-
//TODO implement options
98
pub async fn read_file<'js>(
109
ctx: Ctx<'js>,
1110
path: String,
12-
_options: Opt<Object<'js>>,
13-
) -> Result<Buffer> {
14-
let bytes = fs::read(path.clone())
11+
options: Opt<Object<'js>>,
12+
) -> Result<Value<'js>> {
13+
let mut encoding: String = String::from("");
14+
if let Some(options) = options.0 {
15+
encoding = options.get("encoding").unwrap_or(encoding);
16+
}
17+
18+
let bytes = fs::read(&path)
1519
.await
1620
.or_throw_msg(&ctx, &format!("Can't read \"{}\"", &path))?;
17-
Ok(Buffer(bytes))
21+
22+
// Return string or bytes depending on the option (we only support encoding option now)
23+
return match encoding.to_ascii_lowercase().as_str() {
24+
"" => Buffer(bytes).into_js(&ctx),
25+
"utf-8" => {
26+
let utf8_str = std::str::from_utf8(&bytes).unwrap();
27+
Ok(JsString::from_str(ctx.clone(), utf8_str)?.into_value())
28+
}
29+
_ => Err(Exception::throw_message(
30+
&ctx,
31+
&format!(
32+
"Encoding not supported: {}. Currently supported:utf-8",
33+
encoding
34+
),
35+
)),
36+
};
1837
}

tests/unit/fs.test.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ describe("readfile", () => {
8484
expect(base64Text).toEqual("aGVsbG8gd29ybGQh");
8585
expect(hexText).toEqual("68656c6c6f20776f726c6421");
8686
});
87+
it("should read a file as a string", async () => {
88+
const text = await fs.readFile("fixtures/hello.txt", { encoding: "utf-8" });
89+
90+
expect(typeof text).toEqual("string");
91+
expect(text).toEqual("hello world!");
92+
});
8793
});
8894

8995
describe("mkdtemp", () => {
@@ -106,7 +112,7 @@ describe("mkdtemp", () => {
106112
// Clean up the temporary directory
107113
await fs.rmdir(dirPath);
108114
});
109-
})
115+
});
110116

111117
describe("mkdtempSync", () => {
112118
it("should create a temporary directory with a given prefix synchronously", async () => {
@@ -119,11 +125,11 @@ describe("mkdtempSync", () => {
119125
.stat(dirPath)
120126
.then(() => true)
121127
.catch(() => false);
122-
expect(dirExists).toBeTruthy()
128+
expect(dirExists).toBeTruthy();
123129

124130
// Check that the directory has the correct prefix
125131
const dirPrefix = path.basename(dirPath).slice(0, prefix.length);
126-
expect(dirPrefix).toStrictEqual(prefix)
132+
expect(dirPrefix).toStrictEqual(prefix);
127133

128134
// Clean up the temporary directory
129135
await fs.rmdir(dirPath);
@@ -155,7 +161,9 @@ describe("mkdir", () => {
155161

156162
describe("mkdirSync", () => {
157163
it("should create a directory with the given path synchronously", async () => {
158-
const dirPath = defaultFsImport.mkdtempSync(path.join(os.tmpdir(), "test/test-"));
164+
const dirPath = defaultFsImport.mkdtempSync(
165+
path.join(os.tmpdir(), "test/test-")
166+
);
159167

160168
//non recursive should reject
161169
expect(() => defaultFsImport.mkdirSync(dirPath)).toThrow(/[fF]ile.*exists/);

0 commit comments

Comments
 (0)