Skip to content

Commit

Permalink
feat(ext/fetch): support fetching local files
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Oct 28, 2021
1 parent a065604 commit d612187
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
58 changes: 57 additions & 1 deletion cli/tests/unit/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ unitTest(
unitTest({ permissions: { net: true } }, async function fetchProtocolError() {
await assertRejects(
async () => {
await fetch("file:///");
await fetch("ftp://localhost:21/a/file");
},
TypeError,
"not supported",
Expand Down Expand Up @@ -1360,3 +1360,59 @@ unitTest(
client.close();
},
);

unitTest(async function fetchFilePerm() {
await assertRejects(async () => {
await fetch(new URL("../testdata/subdir/json_1.json", import.meta.url));
}, Deno.errors.PermissionDenied);
});

unitTest(async function fetchFilePermDoesNotExist() {
await assertRejects(async () => {
await fetch(new URL("./bad.json", import.meta.url));
}, Deno.errors.PermissionDenied);
});

unitTest(
{ permissions: { read: true } },
async function fetchFileBadMethod() {
await assertRejects(
async () => {
await fetch(
new URL("../testdata/subdir/json_1.json", import.meta.url),
{
method: "POST",
},
);
},
TypeError,
"Fetching files only supports the GET method. Received POST.",
);
},
);

unitTest(
{ permissions: { read: true } },
async function fetchFileDoesNotExist() {
await assertRejects(
async () => {
await fetch(new URL("./bad.json", import.meta.url));
},
TypeError,
);
},
);

unitTest(
{ permissions: { read: true } },
async function fetchFile() {
const res = await fetch(
new URL("../testdata/subdir/json_1.json", import.meta.url),
);
assert(res.ok);
const fixture = await Deno.readTextFile(
"cli/tests/testdata/subdir/json_1.json",
);
assertEquals(await res.text(), fixture);
},
);
30 changes: 30 additions & 0 deletions ext/fetch/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,36 @@ where
// Check scheme before asking for net permission
let scheme = url.scheme();
let (request_rid, request_body_rid, cancel_handle_rid) = match scheme {
"file" => {
let permissions = state.borrow_mut::<FP>();
let path = url
.to_file_path()
.map_err(|_| type_error(format!("Invalid file URL: {}", url)))?;
permissions.check_read(&path)?;
if method != Method::GET {
return Err(type_error(format!(
"Fetching files only supports the GET method. Received {}.",
method
)));
}
if !path.is_file() {
return Err(type_error(format!("Unable to fetch \"{}\".", url)));
}

let body = std::fs::read(&path)?;

let response = http::Response::builder()
.status(http::StatusCode::OK)
.body(reqwest::Body::from(body))?;

let fut = async move { Ok(Ok(Response::from(response))) };

let request_rid = state
.resource_table
.add(FetchRequestResource(Box::pin(fut)));

(request_rid, None, None)
}
"http" | "https" => {
let permissions = state.borrow_mut::<FP>();
permissions.check_net_url(&url)?;
Expand Down

0 comments on commit d612187

Please sign in to comment.