From 06573e545b90a2539060221e04eae2d9b540152b Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Tue, 26 Oct 2021 14:03:19 +1100 Subject: [PATCH] feat(ext/fetch): support fetching local files Closes #11925 Closes #2150 --- cli/tests/unit/fetch_test.ts | 58 +++++++++++++++++++++++++++++++++++- ext/fetch/lib.rs | 30 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index bc61d67b52da38..98134728e43940 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -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", @@ -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); + }, +); diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index ae81d126cb9c25..5403b7a5cf758d 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -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::(); + 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::(); permissions.check_net_url(&url)?;