Skip to content

feat: add lightweight AssemblyScript SDK for Wagi HTTP components #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

radu-matei
Copy link
Member

@radu-matei radu-matei commented Apr 8, 2022

ref #298

This commit starts the work for a lightweight Spin SDK
for AssemblyScript.
Particularly, it starts the work on defining the HTTP
objects needed, and adds a work-in-progress for reading
request data from the environment (for Wagi components).
This is just a work-in-progress, as reading the request
body and writing the response body are currently not implented.

With this SDK, writing an HTTP component in AssemblyScript:

export function _start(): void {
    handleRequest((request: Request): Response => {
        for (var i = 0; i < request.headers.size; i++) {
            Console.error("Key: " + request.headers.keys()[i]);
            Console.error("Value: " + request.headers.values()[i] + "\n");
        }
        return new ResponseBuilder(StatusCode.FORBIDDEN)
            .header("content-type", "text/plain")
            .header("foo", "bar")
            .body(String.UTF8.encode("Hello, Fermyon!\n"));
    });
}

Note that this still follows the existing convention for
calling the _start function in the module for a Wagi
component.

(We are currently experimenting with going through the C bindings
for writing components in other languages, currently for CGO.
If that experiment is successful, it would allow us, for the list
of languages supported with an SDK, to stop relying on the Wagi
compatibility layer and fully implement all types of components.
That work is still early. Depending on its outcome, we will decide
how to approach the AssemblyScript SDK.)

Signed-off-by: Radu Matei [email protected]

This commit starts the work for a lightweight Spin SDK
for AssemblyScript.
Particularly, it starts the work on defining the HTTP
objects needed, and adds a work-in-progress for reading
request data from the environment (for Wagi components).
This is just a work-in-progress, as reading the request
body and writing the response body are currently not implented.

With this SDK, writing an HTTP component in AssemblyScript:

```typescript
export function _start(): void {
    handleRequest((request: Request): Response => {
        for (var i = 0; i < request.headers.size; i++) {
            Console.error("Key: " + request.headers.keys()[i]);
            Console.error("Value: " + request.headers.values()[i] + "\n");
        }
        return new ResponseBuilder(StatusCode.FORBIDDEN)
            .header("content-type", "text/plain")
            .header("foo", "bar")
            .body(String.UTF8.encode("Hello, Fermyon!\n"));
    });
}
```

Note that this still follows the existing convention for
calling the `_start` function in the module for a Wagi
component.

(We are currently experimenting with going through the C bindings
for writing components in other languages, currently for CGO.
If that experiment is successful, it would allow us, for the list
of languages supported with an SDK, to stop relying on the Wagi
compatibility layer and fully implement all types of components.
That work is still early. Depending on its outcome, we will decide
how to approach the AssemblyScript SDK.)

Signed-off-by: Radu Matei <[email protected]>

export function sendResponse(response: Response): void {
printHeaders(response);
// wasi.Descriptor.Stdout.write(changetype<u8[]>(response.body));
Copy link
Member Author

@radu-matei radu-matei Apr 8, 2022

Choose a reason for hiding this comment

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

Writing the response body is currently not working.

let uri = requestUriFromEnv();
let method = requestMethodFromEnv();
let headers = requestHeadersFromEnv();
// let body = requestBodyFromEnv();
Copy link
Member Author

Choose a reason for hiding this comment

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

Reading the request body is currently not working.

}

/** Convert an HTTP method into a string. */
export function from(m: Method): string {
Copy link
Member Author

Choose a reason for hiding this comment

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

Terrible function name.


export namespace Method {
/** Parse a string into an HTTP method. */
export function parse(m: string): Method {
Copy link
Member Author

Choose a reason for hiding this comment

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

Terrible function name.

@radu-matei radu-matei marked this pull request as draft April 8, 2022 12:30
@radu-matei
Copy link
Member Author

TODO:

  • decide whether this SDK will still follow the Wagi model for HTTP components
  • fix request and response bodies
  • add outbound HTTP
  • add tests

@lann
Copy link
Collaborator

lann commented Apr 8, 2022

I know as is a typical abbreviation in that ecosystem, but maybe give it a longer name in sdk/ to reduce confusion?

@itowlson
Copy link
Collaborator

As a heads up, one thing to test for in this is handling of the special _routes entry point. They believe they have fixed the issue with non-default entry points, but from their description (AssemblyScript/assemblyscript#2157) I'm not sure it will work for WAGI.

(And this may be on us. WAGI doesn't really treat the module as a reactor, but instead kinda treats it as two commands in a trenchcoat, which works okay with Rust but might be a bit ambiguous in terms of the WASI spec, I'm not sure.)

@radu-matei
Copy link
Member Author

Oh, this brings up an excellent point that we (read I) should have documented and/or implemented — see #342

@radu-matei
Copy link
Member Author

AssemblyScript recently removed WASI support from the core project, and we no longer support the old WASI experimental HTTP library, so defering this SDK for now.

Happy to revisit if people are interested.

ref #699

@radu-matei radu-matei closed this Aug 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants