Skip to content
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

Rust: Update WebAssembly Example #5171

Merged
merged 4 commits into from
Sep 6, 2023
Merged
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
3 changes: 3 additions & 0 deletions rust_dev_preview/webassembly/Cargo.toml
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ aws-sdk-lambda = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "ne
aws-smithy-client = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", default-features = false }
aws-smithy-http = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next", features = ["event-stream"] }
aws-smithy-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" }
aws-smithy-async = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "next" }
async-trait = "0.1.63"
console_error_panic_hook = "0.1.7"
http = "0.2.8"
@@ -27,6 +28,8 @@ tower = "0.4.13"
wasm-bindgen = "0.2.83"
wasm-bindgen-futures = "0.4.33"
wasm-timer = "0.2.5"
tracing-wasm = "0.2.1"
tracing = "0.1.37"

[dependencies.web-sys]
version = "0.3.60"
33 changes: 20 additions & 13 deletions rust_dev_preview/webassembly/src/lib.rs
Original file line number Diff line number Diff line change
@@ -4,13 +4,16 @@
*/

use async_trait::async_trait;
use aws_credential_types::{cache::CredentialsCache, provider::ProvideCredentials, Credentials};
use aws_sdk_lambda::config::{AsyncSleep, Region, SharedAsyncSleep, Sleep};
use aws_credential_types::{provider::ProvideCredentials, Credentials};
use aws_sdk_lambda::config::{AsyncSleep, Region, Sleep};
use aws_sdk_lambda::primitives::SdkBody;
use aws_sdk_lambda::{meta::PKG_VERSION, Client};
use aws_smithy_async::time::TimeSource;
use aws_smithy_http::result::ConnectorError;
use serde::Deserialize;
use std::time::SystemTime;
use wasm_bindgen::{prelude::*, JsCast};
use wasm_timer::UNIX_EPOCH;

macro_rules! log {
( $( $t:tt )* ) => {
@@ -36,7 +39,8 @@ extern "C" {

#[wasm_bindgen(start)]
pub fn start() {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
console_error_panic_hook::set_once();
tracing_wasm::set_as_global_default();
log!("initializing module...");
}

@@ -57,16 +61,14 @@ pub async fn main(region: String, verbose: bool) -> Result<String, String> {
let shared_config = aws_config::from_env()
.sleep_impl(BrowserSleep)
.region(Region::new(region))
.credentials_cache(browser_credentials_cache())
.time_source(BrowserNow)
.credentials_provider(credentials_provider)
.http_connector(Adapter::new(verbose, access_key == "access_key"))
.load()
.await;
tracing::info!("sdk config: {:#?}", shared_config);
let client = Client::new(&shared_config);

let now = std::time::Duration::new(now() as u64, 0);
log!("current date in unix timestamp: {}", now.as_secs());

let resp = client
.list_functions()
.send()
@@ -85,6 +87,17 @@ pub async fn main(region: String, verbose: bool) -> Result<String, String> {
Ok(output)
}

#[derive(Debug)]
struct BrowserNow;
impl TimeSource for BrowserNow {
fn now(&self) -> SystemTime {
let offset = wasm_timer::SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap();
std::time::UNIX_EPOCH + offset
}
}

#[derive(Debug, Clone)]
struct BrowserSleep;
impl AsyncSleep for BrowserSleep {
@@ -105,12 +118,6 @@ fn static_credential_provider() -> impl ProvideCredentials {
)
}

fn browser_credentials_cache() -> CredentialsCache {
CredentialsCache::lazy_builder()
.sleep(SharedAsyncSleep::new(BrowserSleep))
.into_credentials_cache()
}

/// At this moment, there is no standard mechanism to make an outbound
/// HTTP request from within the guest Wasm module.
/// Eventually that will be defined by the WebAssembly System Interface:
359 changes: 154 additions & 205 deletions rust_dev_preview/webassembly/www/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions rust_dev_preview/webassembly/www/package.json
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
},
"devDependencies": {
"html-webpack-plugin": "^5.5.0",
"webpack": "^5.88.2",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.11.1"
}