Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam McKee committed Jan 1, 2025
1 parent 3077d57 commit 2b378e3
Show file tree
Hide file tree
Showing 14 changed files with 2,055 additions and 63 deletions.
1,877 changes: 1,814 additions & 63 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"poc_config/aws_wasi_host",
"fn_build",
"l3_base",
"l3_cli",
Expand Down
9 changes: 9 additions & 0 deletions poc_config/aws_wasi_guest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## validate wit file

`cat ./aws.wit | wasm-tools component wit`

## generate js types

npx @bytecodealliance/jco types ./ -o bindings

## build component
33 changes: 33 additions & 0 deletions poc_config/aws_wasi_guest/aws.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package l3:configure;

world configure {

variant api-instance {
create-new,
use-api-id(string),
}

record api-gateway-config {
api: api-instance
}

record load-balancer-config {
}

variant cloudfront-origin {
api-gateway(api-gateway-config),
application-load-balancer(load-balancer-config),
}

record cloudfront-config {
origin: cloudfront-origin,
}

variant http-config {
api-gateway(api-gateway-config),
application-load-balancer(load-balancer-config),
cloudfront-origin(cloudfront-config),
}

export configure: func() -> http-config;
}
39 changes: 39 additions & 0 deletions poc_config/aws_wasi_guest/bindings/..d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export type ApiInstance = ApiInstanceCreateNew | ApiInstanceUseApiId;
export interface ApiInstanceCreateNew {
tag: 'create-new',
}
export interface ApiInstanceUseApiId {
tag: 'use-api-id',
val: string,
}
export interface ApiGatewayConfig {
api: ApiInstance,
}
export interface LoadBalancerConfig {
}
export type CloudfrontOrigin = CloudfrontOriginApiGateway | CloudfrontOriginApplicationLoadBalancer;
export interface CloudfrontOriginApiGateway {
tag: 'api-gateway',
val: ApiGatewayConfig,
}
export interface CloudfrontOriginApplicationLoadBalancer {
tag: 'application-load-balancer',
val: LoadBalancerConfig,
}
export interface CloudfrontConfig {
origin: CloudfrontOrigin,
}
export type HttpConfig = HttpConfigApiGateway | HttpConfigApplicationLoadBalancer | HttpConfigCloudfrontOrigin;
export interface HttpConfigApiGateway {
tag: 'api-gateway',
val: ApiGatewayConfig,
}
export interface HttpConfigApplicationLoadBalancer {
tag: 'application-load-balancer',
val: LoadBalancerConfig,
}
export interface HttpConfigCloudfrontOrigin {
tag: 'cloudfront-origin',
val: CloudfrontConfig,
}
export function configure(): HttpConfig;
13 changes: 13 additions & 0 deletions poc_config/aws_wasi_guest/configure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @return {import('./bindings/..d.ts').HttpConfig}
*/
export function configure() {
return {
tag: 'api-gateway',
val: {
api: {
tag: 'create-new',
},
},
}
}
18 changes: 18 additions & 0 deletions poc_config/aws_wasi_host/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "aws_wasi"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
async-std = { version = "1.13.0", features = ["attributes"] }
cap-std = { version = "3.3.0 "}
thiserror = { workspace = true }
wasmtime = { version = "25.0.1", features = ["component-model"]}
wasmtime-wasi = "25.0.1"
wasmtime-wasi-http = "25.0.1"
anyhow = "1.0.92"


33 changes: 33 additions & 0 deletions poc_config/aws_wasi_host/aws.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package l3:configure;

world configure {

variant api-instance {
create-new,
use-api-id(string),
}

record api-gateway-config {
api: api-instance
}

record load-balancer-config {
}

variant cloudfront-origin {
api-gateway(api-gateway-config),
application-load-balancer(load-balancer-config),
}

record cloudfront-config {
origin: cloudfront-origin,
}

variant http-config {
api-gateway(api-gateway-config),
application-load-balancer(load-balancer-config),
cloudfront-origin(cloudfront-config),
}

export configure: func() -> http-config;
}
70 changes: 70 additions & 0 deletions poc_config/aws_wasi_host/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use anyhow::Result;
use wasmtime::{
component::{Component, Linker},
Config, Engine, Store, WasmBacktraceDetails,
};
use wasmtime_wasi::{ResourceTable, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};

wasmtime::component::bindgen!({
world: "configure",
path: "aws.wit",
async: true
});

#[async_std::main]
async fn main() -> Result<()> {
let mut builder = WasiCtxBuilder::new();
builder.inherit_stdio();
let table = ResourceTable::new();
let wasi = builder.build();

let mut config = Config::new();
config.cache_config_load_default().unwrap();
config.wasm_backtrace_details(WasmBacktraceDetails::Enable);
config.wasm_component_model(true);
config.async_support(true);

let engine = Engine::new(&config)?;
let mut linker = Linker::new(&engine);

let component = Component::from_file(&engine, "hello.component.wasm").unwrap();

struct CommandExtendedCtx {
table: ResourceTable,
wasi: WasiCtx,
http: WasiHttpCtx,
}
impl WasiView for CommandExtendedCtx {
fn table(&mut self) -> &mut ResourceTable {
&mut self.table
}
fn ctx(&mut self) -> &mut WasiCtx {
&mut self.wasi
}
}
impl WasiHttpView for CommandExtendedCtx {
fn table(&mut self) -> &mut ResourceTable {
&mut self.table
}
fn ctx(&mut self) -> &mut WasiHttpCtx {
&mut self.http
}
}

wasmtime_wasi::add_to_linker_sync(&mut linker)?;
wasmtime_wasi_http::add_only_http_to_linker_sync(&mut linker)?;
let mut store = Store::new(
&engine,
CommandExtendedCtx {
table,
wasi,
http: WasiHttpCtx::new(),
},
);

let hello = Configure::instantiate_async(&mut store, &component, &linker).await?;
let res = hello.call_configure(&mut store).await?;
dbg!(res);
Ok(())
}
4 changes: 4 additions & 0 deletions poc_config/sdk_js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "l3-config",
"version": "0.0.1"
}
16 changes: 16 additions & 0 deletions poc_config/sdk_js/src/config/Aws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {LambdaPlatform} from './LambdaPlatform'

export class ApiGateway {
readonly tag: string = 'api-gateway'
}

export class LoadBalancer {
readonly tag: string = 'application-load-balancer'
}

export type HttpRouting = ApiGateway | LoadBalancer

export class Platform extends LambdaPlatform<HttpRouting> {
httpRouting(routing: HttpRouting) {
}
}
3 changes: 3 additions & 0 deletions poc_config/sdk_js/src/config/LambdaPlatform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export abstract class LambdaPlatform<R> {
abstract httpRouting(routing: R)
}
2 changes: 2 additions & 0 deletions poc_config/sdk_js/src/config/l3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

import {AWS} from './Aws.js'
Empty file added poc_config/sdk_js/tsconfig.json
Empty file.

0 comments on commit 2b378e3

Please sign in to comment.