diff options
author | Luca Casonato <lucacasonato@yahoo.com> | 2021-04-07 15:22:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-07 15:22:14 +0200 |
commit | 966ce7de8a23f63d0f30b1748fe69ccaf07519e0 (patch) | |
tree | 3275ca96a835fe91a62a73d5a4c83bf6ca917b66 /cli/tsc.rs | |
parent | 2865f39bec6da135a2d2d679a65e7ff139131bd7 (diff) |
feat: blob URL support (#10045)
This commit adds blob URL support. Blob URLs are stored in a process
global storage, that can be accessed from all workers, and the module
loader. Blob URLs can be created using `URL.createObjectURL` and revoked
using `URL.revokeObjectURL`.
This commit does not add support for `fetch`ing blob URLs. This will be
added in a follow up commit.
Diffstat (limited to 'cli/tsc.rs')
-rw-r--r-- | cli/tsc.rs | 48 |
1 files changed, 35 insertions, 13 deletions
diff --git a/cli/tsc.rs b/cli/tsc.rs index 7c54905c5..834b0cc58 100644 --- a/cli/tsc.rs +++ b/cli/tsc.rs @@ -110,6 +110,19 @@ fn hash_data_url( format!("data:///{}{}", hash, media_type.as_ts_extension()) } +fn hash_blob_url( + specifier: &ModuleSpecifier, + media_type: &MediaType, +) -> String { + assert_eq!( + specifier.scheme(), + "blob", + "Specifier must be a blob: specifier." + ); + let hash = crate::checksum::gen(&[specifier.path().as_bytes()]); + format!("blob:///{}{}", hash, media_type.as_ts_extension()) +} + /// tsc only supports `.ts`, `.tsx`, `.d.ts`, `.js`, or `.jsx` as root modules /// and so we have to detect the apparent media type based on extensions it /// supports. @@ -378,15 +391,19 @@ fn resolve(state: &mut State, args: Value) -> Result<Value, AnyError> { resolved_specifier ) }; - let resolved_specifier_str = if resolved_specifier.scheme() == "data" - { - let specifier_str = hash_data_url(&resolved_specifier, &media_type); - state - .data_url_map - .insert(specifier_str.clone(), resolved_specifier); - specifier_str - } else { - resolved_specifier.to_string() + let resolved_specifier_str = match resolved_specifier.scheme() { + "data" | "blob" => { + let specifier_str = if resolved_specifier.scheme() == "data" { + hash_data_url(&resolved_specifier, &media_type) + } else { + hash_blob_url(&resolved_specifier, &media_type) + }; + state + .data_url_map + .insert(specifier_str.clone(), resolved_specifier); + specifier_str + } + _ => resolved_specifier.to_string(), }; resolved.push(( resolved_specifier_str, @@ -439,12 +456,17 @@ pub fn exec(request: Request) -> Result<Response, AnyError> { let root_names: Vec<String> = request .root_names .iter() - .map(|(s, mt)| { - if s.scheme() == "data" { - let specifier_str = hash_data_url(s, mt); + .map(|(s, mt)| match s.scheme() { + "data" | "blob" => { + let specifier_str = if s.scheme() == "data" { + hash_data_url(&s, &mt) + } else { + hash_blob_url(&s, &mt) + }; data_url_map.insert(specifier_str.clone(), s.clone()); specifier_str - } else { + } + _ => { let ext_media_type = get_tsc_media_type(s); if mt != &ext_media_type { let new_specifier = format!("{}{}", s, mt.as_ts_extension()); |