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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules
ignored-assets
main.wasm
.parcel-cache
build/
1 change: 0 additions & 1 deletion crates/config/src/provider/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ impl Provider for EnvProvider {
#[cfg(test)]
mod test {
use super::*;

#[test]
fn provider_get() {
std::env::set_var("TESTING_SPIN_ENV_KEY1", "val");
Expand Down
15 changes: 15 additions & 0 deletions examples/http-assemblyscript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Console } from 'as-wasi';
import { handleRequest, Request, Response, ResponseBuilder, StatusCode } from '../../sdk/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"));
});
}
78 changes: 78 additions & 0 deletions examples/http-assemblyscript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions examples/http-assemblyscript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@fermyon/spin-assemblyscript-sample",
"main": "index.ts",
"ascMain": "index.ts",
"types": "index.ts",
"version": "0.1.0",
"description": "A lightweight Spin HTTP handler in AssemblyScript",
"author": {
"name": "Fermyon Engineering",
"email": "[email protected]"
},
"license": "Apache V2",
"scripts": {
"asbuild": "asc index.ts -b build/optimized.wasm --use abort=wasi_abort --debug"
},
"dependencies": {
"as-wasi": "0.4.4",
"assemblyscript": "^0.18.16"
}
}
13 changes: 13 additions & 0 deletions examples/http-assemblyscript/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
spin_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "A simple Spin application written in AssemblyScript"
name = "spinass"
trigger = { type = "http", base = "/" }
version = "1.0.0"

[[component]]
id = "as"
source = "build/optimized.wasm"
[component.trigger]
route = "/hello"
executor = { type = "wagi" }
6 changes: 6 additions & 0 deletions examples/http-assemblyscript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./node_modules/assemblyscript/std/assembly.json",
"include": [
"./**/*.ts"
]
}
85 changes: 85 additions & 0 deletions sdk/assemblyscript/http/cgi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as wasi from 'as-wasi';
import { Method, Request, Response } from "./http";

export function requestFromCgi(): Request {
let uri = requestUriFromEnv();
let method = requestMethodFromEnv();
let headers = requestHeadersFromEnv();
// let body = requestBodyFromEnv();

return new Request(uri, method, headers);
}

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

function requestHeadersFromEnv(): Map<string, string> {
let res = new Map<string, string>();
let env = new wasi.Environ().all();

for (var i = 0; i < env.length; i++) {
// only set as request headers the environment variables that start with HTTP_
if (env[i].key.startsWith("HTTP_")) {
res.set(env[i].key.replace("HTTP_", "").toLowerCase(), env[i].value.toLowerCase());
}
}

return res;
}

/** Return the request body from the standard input. */
function requestBodyFromEnv(): ArrayBuffer {
let bytes = wasi.Descriptor.Stdin.readAll() || [];
if (bytes !== null) {
wasi.Console.error("Body size: " + bytes.length.toString());
}
return changetype<ArrayBuffer>(bytes);
}

function requestUriFromEnv(): string {
let url = new wasi.Environ().get("X_FULL_URL");
if (url !== null) {
return url;
} else {
return "";
}
}

function requestMethodFromEnv(): Method {
let method = new wasi.Environ().get("REQUEST_METHOD");
if (method !== null) {
return Method.parse(method);
} else {
return Method.GET;
}
}


function printHeaders(response: Response): void {
let location = searchCaseInsensitive("location", response.headers);
if (location !== null) {
wasi.Console.write("Location: " + location, true);
}
let contentType = searchCaseInsensitive("content-type", response.headers);
if (contentType !== null) {
wasi.Console.write("Content-Type: " + contentType, true);
}

for (var i = 0; i < response.headers.size; i++) {
wasi.Console.write(response.headers.keys()[i] + ": " + response.headers.values()[i]);
}
wasi.Console.write("Status: " + response.status.toString());
wasi.Console.write("\n");
}

function searchCaseInsensitive(key: string, map: Map<string, string>): string | null {
for (var i = 0; i < map.size; i++) {
if (map.keys()[i].toLowerCase() === key.toLowerCase()) {
return map.values()[i].toLowerCase();
}
}

return null;
}
Loading