diff options
Diffstat (limited to 'ext/fetch')
-rw-r--r-- | ext/fetch/23_request.js | 8 | ||||
-rw-r--r-- | ext/fetch/26_fetch.js | 28 | ||||
-rw-r--r-- | ext/fetch/lib.rs | 47 |
3 files changed, 39 insertions, 44 deletions
diff --git a/ext/fetch/23_request.js b/ext/fetch/23_request.js index 1372125c1..36c63db2a 100644 --- a/ext/fetch/23_request.js +++ b/ext/fetch/23_request.js @@ -19,6 +19,7 @@ const { mixinBody, extractBody } = window.__bootstrap.fetchBody; const { getLocationHref } = window.__bootstrap.location; const mimesniff = window.__bootstrap.mimesniff; + const { blobFromObjectUrl } = window.__bootstrap.file; const { headersFromHeaderList, headerListFromHeaders, @@ -59,6 +60,7 @@ * @property {number} redirectCount * @property {string[]} urlList * @property {number | null} clientRid NOTE: non standard extension for `Deno.HttpClient`. + * @property {Blob | null} blobUrlEntry */ const defaultInnerRequest = { @@ -81,11 +83,16 @@ * @returns */ function newInnerRequest(method, url, headerList = [], body = null) { + let blobUrlEntry = null; + if (url.startsWith("blob:")) { + blobUrlEntry = blobFromObjectUrl(url); + } return { method: method, headerList, body, urlList: [url], + blobUrlEntry, ...defaultInnerRequest, }; } @@ -118,6 +125,7 @@ redirectCount: request.redirectCount, urlList: request.urlList, clientRid: request.clientRid, + blobUrlEntry: request.blobUrlEntry, }; } diff --git a/ext/fetch/26_fetch.js b/ext/fetch/26_fetch.js index edd14abf1..ada524fcb 100644 --- a/ext/fetch/26_fetch.js +++ b/ext/fetch/26_fetch.js @@ -35,6 +35,7 @@ Promise, PromisePrototypeThen, PromisePrototypeCatch, + String, StringPrototypeToLowerCase, TypedArrayPrototypeSubarray, TypeError, @@ -172,6 +173,33 @@ * @returns {Promise<InnerResponse>} */ async function mainFetch(req, recursive, terminator) { + if (req.blobUrlEntry !== null) { + if (req.method !== "GET") { + throw new TypeError("Blob URL fetch only supports GET method."); + } + + const body = new InnerBody(req.blobUrlEntry.stream()); + terminator[abortSignal.add](() => + body.error(new DOMException("Ongoing fetch was aborted.", "AbortError")) + ); + + return { + headerList: [ + ["content-length", String(req.blobUrlEntry.size)], + ["content-type", req.blobUrlEntry.type], + ], + status: 200, + statusMessage: "OK", + body, + type: "basic", + url() { + if (this.urlList.length == 0) return null; + return this.urlList[this.urlList.length - 1]; + }, + urlList: recursive ? [] : [...req.urlList], + }; + } + /** @type {ReadableStream<Uint8Array> | Uint8Array | null} */ let reqBody = null; diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index c419180c5..70ed40358 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -26,7 +26,6 @@ use deno_core::ZeroCopyBuf; use deno_tls::create_http_client; use deno_tls::rustls::RootCertStore; use deno_tls::Proxy; -use deno_web::BlobStore; use http::header::CONTENT_LENGTH; use reqwest::header::HeaderName; use reqwest::header::HeaderValue; @@ -275,49 +274,9 @@ where (request_rid, None, None) } "blob" => { - let blob_store = state.try_borrow::<BlobStore>().ok_or_else(|| { - type_error("Blob URLs are not supported in this context.") - })?; - - let blob = blob_store - .get_object_url(url)? - .ok_or_else(|| type_error("Blob for the given URL not found."))?; - - if method != "GET" { - return Err(type_error("Blob URL fetch only supports GET method.")); - } - - let cancel_handle = CancelHandle::new_rc(); - let cancel_handle_ = cancel_handle.clone(); - - let fut = async move { - // TODO(lucacsonato): this should be a stream! - let chunk = match blob.read_all().or_cancel(cancel_handle_).await? { - Ok(chunk) => chunk, - Err(err) => return Ok(Err(err)), - }; - - let res = http::Response::builder() - .status(http::StatusCode::OK) - .header(http::header::CONTENT_LENGTH, chunk.len()) - .header(http::header::CONTENT_TYPE, blob.media_type.clone()) - .body(reqwest::Body::from(chunk)) - .map_err(|err| type_error(err.to_string())); - - match res { - Ok(response) => Ok(Ok(Response::from(response))), - Err(err) => Ok(Err(err)), - } - }; - - let request_rid = state - .resource_table - .add(FetchRequestResource(Box::pin(fut))); - - let cancel_handle_rid = - state.resource_table.add(FetchCancelHandle(cancel_handle)); - - (request_rid, None, Some(cancel_handle_rid)) + // Blob URL resolution happens in the JS side of fetch. If we got here is + // because the URL isn't an object URL. + return Err(type_error("Blob for the given URL not found.")); } _ => return Err(type_error(format!("scheme '{}' not supported", scheme))), }; |