这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@Angelmmiguel
Copy link
Contributor

Allow Rust workers to return an array of bytes instead of a String. Note that wws serializes the worker output as an UTF-8 JSON, so an arbitrary array of bytes may not be serializable. For this reason, we implemented this feature using base64 to encode the output when the worker exports an array of bytes.

Then, wws will look for the base64 flag in the serialized JSON. If it's true, it will perform the decoding process before sending the result back.

The feature introduces a breaking change as we cannot now set the response body type as String. I created a new Content enum that can represents both text and base64 responses.

The change is pretty trivial:

# Before
fn handler(_req: Request<String>, cache: &mut Cache) -> Result<Response<String>>;

# After
fn handler(_req: Request<String>, cache: &mut Cache) -> Result<Response<Content>>;

Thanks to the implementation of the From trait, the way you pass the body to the response will continue as it's now:

Ok(http::Response::builder()
    .status(200)
    .header("x-generated-by", "wasm-workers-server")
    .body(response.into())?)

The statement response.into() will initialize the right Content enum type.

The PR also updates the documentation and adds a new fun example called pdf-create.

It closes #44

@Angelmmiguel Angelmmiguel added 🚀 enhancement New feature or request 🚨 breaking change Introduces a breaking change in the project labels Nov 28, 2022
@Angelmmiguel Angelmmiguel self-assigned this Nov 28, 2022
@gzurl
Copy link
Contributor

gzurl commented Nov 28, 2022

LGTM! Just a minor comment.

Copy link
Contributor

@ereslibre ereslibre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new feature with more idiomatic code here and there, great work! 👏 🚢

Minor comments!

@Angelmmiguel Angelmmiguel merged commit 90e11f6 into main Nov 29, 2022
@Angelmmiguel Angelmmiguel deleted the 44-allow-returning-bytes-from-worker branch May 8, 2023 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🚨 breaking change Introduces a breaking change in the project cla-not-required 🚀 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow Rust workers to return an array of bytes too

5 participants