Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Change the API to allow streaming & no callback to alloc #43

Merged
merged 3 commits into from
Mar 30, 2021

Conversation

jedisct1
Copy link
Contributor

Hi!

This is a proposal to support streaming (at least in the API and for responses), avoid the alloc function, and simplify the current implementation while making it closer to other WASI proposals.

req() now returns a handle to a Response object, along with the status code.

A Response object can be then used to:

  • Read individual headers
  • Read the body in a streaming fashion.

A body_read() function pulls as many bytes as the client wants, and returns 0 when the end of the stream is reached.
This makes it compatible with streaming, doesn't require any dynamic memory allocation, and allows the client to control the maximum response size it's willing to accept.

For conveniency, the Rust and AssemblyScript bindings also have a readAll function that reads everything until the end and puts that into a dynamically allocated array.

Usage in an app using wasmtime is now similar to wasi (core) and other WASI proposals, with an object and an add_to_linker() function.

Errors are a little bit easier to keep track of, by using Rust errors, that are converted to return codes when needed. WITX is supposed to provide eventually provide the magic to get error strings from that.

Dependencies have been updated, especially wasmtime.

This makes a couple changes to the API to make it closer to other
WASI APIs.

`req()` returns a handle to a `Response` object, along with the status
code.

A `Response` object can be then used to:
- Read individual headers
- Read the body in a streaming fashion.

A `body_read()` function pulls as many bytes as the client wants,
and returns `0` when the end of the stream is reached.
This makes it compatible with streaming, doesn't require any dynamic
memory allocation, and allows the client to control the maximum
response size it's willing to accept.

For conveniency, the Rust and AssemblyScript bindings also have a
`readAll` function that reads everything until the end and puts that
into a dynamically allocated array.

Usage in an app using wasmtime is now similar to wasi (core) and other
WASI proposals, with an object and an `add_to_linker()` function.

Errors are a little bit easier to keep track of, by using Rust errors,
that are converted to return codes when needed.
WITX is supposed to provide eventually provide the magic to get error
strings from that.

Dependencies have been updated, especially `wasmtime`.
@jedisct1
Copy link
Contributor Author

Fixes #17
Fixes #26
Fixes #32

@radu-matei
Copy link
Member

This is great, @jedisct1!
I do have a question about the key of the responses hash map, but I really like the approach this is taking!

@radu-matei
Copy link
Member

Could you please add some comments to the exported items?

@jedisct1
Copy link
Contributor Author

Could you please add some comments to the exported items?

Of course!

Done!

Copy link
Member

@radu-matei radu-matei left a comment

Choose a reason for hiding this comment

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

This is great!
I do have a few follow-up PRs that I will open, but at first they will be mostly cosmetic.

Thank you so much for this, @jedisct1!

@radu-matei radu-matei merged commit 0cf7496 into deislabs:main Mar 30, 2021
@jedisct1 jedisct1 deleted the newapi branch March 30, 2021 16:53
This was referenced Mar 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants