diff options
author | Andreu Botella <abb@randomunok.com> | 2021-09-08 11:29:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-08 11:29:21 +0200 |
commit | 1563088f060b1f1d4826bb3a6d4120b276a4e15f (patch) | |
tree | ff8b406511f0e7322a55e32d5272298ea4a061b5 /ext/web/blob.rs | |
parent | e07f28d301b990ebf534cbb8d5fa9f507475c89f (diff) |
fix: a `Request` whose URL is a revoked blob URL should still fetch (#11947)
In the spec, a URL record has an associated "blob URL entry", which for
`blob:` URLs is populated during parsing to contain a reference to the
`Blob` object that backs that object URL. It is this blob URL entry that
the `fetch` API uses to resolve an object URL.
Therefore, since the `Request` constructor parses URL inputs, it will
have an associated blob URL entry which will be used when fetching, even
if the object URL has been revoked since the construction of the
`Request` object. (The `Request` constructor takes the URL as a string
and parses it, so the object URL must be live at the time it is called.)
This PR adds a new `blobFromObjectUrl` JS function (backed by a new
`op_blob_from_object_url` op) that, if the URL is a valid object URL,
returns a new `Blob` object whose parts are references to the same Rust
`BlobPart`s used by the original `Blob` object. It uses this function to
add a new `blobUrlEntry` field to inner requests, which will be `null`
or such a `Blob`, and then uses `Blob.prototype.stream()` as the
response's body. As a result of this, the `blob:` URL resolution from
`op_fetch` is now useless, and has been removed.
Diffstat (limited to 'ext/web/blob.rs')
-rw-r--r-- | ext/web/blob.rs | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/ext/web/blob.rs b/ext/web/blob.rs index 2179de4a6..37e93c853 100644 --- a/ext/web/blob.rs +++ b/ext/web/blob.rs @@ -3,7 +3,7 @@ use deno_core::error::type_error; use deno_core::parking_lot::Mutex; use deno_core::url::Url; use deno_core::ZeroCopyBuf; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use std::cell::RefCell; use std::collections::HashMap; use std::fmt::Debug; @@ -260,3 +260,46 @@ pub fn op_blob_revoke_object_url( blob_store.remove_object_url(&url); Ok(()) } + +#[derive(Serialize)] +pub struct ReturnBlob { + pub media_type: String, + pub parts: Vec<ReturnBlobPart>, +} + +#[derive(Serialize)] +pub struct ReturnBlobPart { + pub uuid: Uuid, + pub size: usize, +} + +pub fn op_blob_from_object_url( + state: &mut deno_core::OpState, + url: String, + _: (), +) -> Result<Option<ReturnBlob>, AnyError> { + let url = Url::parse(&url)?; + if url.scheme() != "blob" { + return Ok(None); + } + + let blob_store = state.try_borrow::<BlobStore>().ok_or_else(|| { + type_error("Blob URLs are not supported in this context.") + })?; + if let Some(blob) = blob_store.get_object_url(url)? { + let parts = blob + .parts + .iter() + .map(|part| ReturnBlobPart { + uuid: blob_store.insert_part(part.clone()), + size: part.size(), + }) + .collect(); + Ok(Some(ReturnBlob { + media_type: blob.media_type.clone(), + parts, + })) + } else { + Ok(None) + } +} |