Skip to content

Commit 56c3da3

Browse files
sokraalexkirsz
andauthored
Image processing improvements (vercel#4649)
### Description * refactor image processing * fallback guess format for extension * expose pathname_to_url for wrapped source usage see also vercel/next.js#48622 --------- Co-authored-by: Alex Kirszenberg <alex.kirszenberg@vercel.com>
1 parent 4058da0 commit 56c3da3

File tree

4 files changed

+213
-89
lines changed

4 files changed

+213
-89
lines changed

crates/turbo-tasks/src/task_input.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ pub enum TaskInput {
330330
String(String),
331331
Bool(bool),
332332
Usize(usize),
333+
I8(i8),
334+
U8(u8),
333335
I16(i16),
334336
U16(u16),
335337
I32(i32),
@@ -515,6 +517,8 @@ impl Display for TaskInput {
515517
TaskInput::String(s) => write!(f, "string {:?}", s),
516518
TaskInput::Bool(b) => write!(f, "bool {:?}", b),
517519
TaskInput::Usize(v) => write!(f, "usize {}", v),
520+
TaskInput::I8(v) => write!(f, "i8 {}", v),
521+
TaskInput::U8(v) => write!(f, "u8 {}", v),
518522
TaskInput::I16(v) => write!(f, "i16 {}", v),
519523
TaskInput::U16(v) => write!(f, "u16 {}", v),
520524
TaskInput::I32(v) => write!(f, "i32 {}", v),
@@ -548,6 +552,18 @@ impl From<bool> for TaskInput {
548552
}
549553
}
550554

555+
impl From<i8> for TaskInput {
556+
fn from(v: i8) -> Self {
557+
TaskInput::I8(v)
558+
}
559+
}
560+
561+
impl From<u8> for TaskInput {
562+
fn from(v: u8) -> Self {
563+
TaskInput::U8(v)
564+
}
565+
}
566+
551567
impl From<i16> for TaskInput {
552568
fn from(v: i16) -> Self {
553569
TaskInput::I16(v)
@@ -694,6 +710,28 @@ impl<'a, T: FromTaskInput<'a, Error = anyhow::Error>> FromTaskInput<'a> for Vec<
694710
}
695711
}
696712

713+
impl FromTaskInput<'_> for u8 {
714+
type Error = anyhow::Error;
715+
716+
fn try_from(value: &TaskInput) -> Result<Self, Self::Error> {
717+
match value {
718+
TaskInput::U8(value) => Ok(*value),
719+
_ => Err(anyhow!("invalid task input type, expected u8")),
720+
}
721+
}
722+
}
723+
724+
impl FromTaskInput<'_> for i8 {
725+
type Error = anyhow::Error;
726+
727+
fn try_from(value: &TaskInput) -> Result<Self, Self::Error> {
728+
match value {
729+
TaskInput::I8(value) => Ok(*value),
730+
_ => Err(anyhow!("invalid task input type, expected i8")),
731+
}
732+
}
733+
}
734+
697735
impl FromTaskInput<'_> for u16 {
698736
type Error = anyhow::Error;
699737

crates/turbopack-dev-server/src/source/source_maps.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::{borrow::Cow, iter::once};
2-
31
use anyhow::Result;
42
use mime::APPLICATION_JSON;
53
use turbo_tasks::{primitives::StringVc, Value};
@@ -12,7 +10,10 @@ use turbopack_core::{
1210

1311
use super::{
1412
query::QueryValue,
15-
wrapping_source::{ContentSourceProcessor, ContentSourceProcessorVc, WrappedContentSourceVc},
13+
wrapping_source::{
14+
encode_pathname_to_url, ContentSourceProcessor, ContentSourceProcessorVc,
15+
WrappedContentSourceVc,
16+
},
1617
ContentSource, ContentSourceContent, ContentSourceContentVc, ContentSourceData,
1718
ContentSourceDataFilter, ContentSourceDataVary, ContentSourceResultVc, ContentSourceVc,
1819
NeededData, RewriteBuilder,
@@ -42,17 +43,6 @@ impl SourceMapContentSourceVc {
4243
}
4344
}
4445

45-
fn encode_pathname_to_url(pathname: &str) -> String {
46-
once(Cow::Borrowed("/"))
47-
.chain(
48-
pathname
49-
.split('/')
50-
.map(urlencoding::encode)
51-
.intersperse(Cow::Borrowed("/")),
52-
)
53-
.collect()
54-
}
55-
5646
#[turbo_tasks::value_impl]
5747
impl ContentSource for SourceMapContentSource {
5848
#[turbo_tasks::function]

crates/turbopack-dev-server/src/source/wrapping_source.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::{borrow::Cow, iter::once};
2+
13
use anyhow::Result;
24
use turbo_tasks::Value;
35

@@ -20,6 +22,17 @@ pub trait ContentSourceProcessor {
2022
fn process(&self, content: ContentSourceContentVc) -> ContentSourceContentVc;
2123
}
2224

25+
pub fn encode_pathname_to_url(pathname: &str) -> String {
26+
once(Cow::Borrowed("/"))
27+
.chain(
28+
pathname
29+
.split('/')
30+
.map(urlencoding::encode)
31+
.intersperse(Cow::Borrowed("/")),
32+
)
33+
.collect()
34+
}
35+
2336
/// A ContentSourceProcessor allows a [ContentSource] implementation to easily
2437
/// register a final process step over some inner ContentSource's fully resolved
2538
/// [ContentSourceResult] and [ContentSourceContent] without having to manually

0 commit comments

Comments
 (0)