Skip to content

Commit

Permalink
Update WASM Example for new SDK updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh committed Aug 28, 2023
1 parent eececa5 commit bfebab1
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 218 deletions.
3 changes: 3 additions & 0 deletions rust_dev_preview/webassembly/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
31 changes: 18 additions & 13 deletions rust_dev_preview/webassembly/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
* SPDX-License-Identifier: Apache-2.0.
*/

use std::time::SystemTime;
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_smithy_async::time::TimeSource;
use aws_sdk_lambda::primitives::SdkBody;
use aws_sdk_lambda::{meta::PKG_VERSION, Client};
use aws_smithy_http::result::ConnectorError;
use serde::Deserialize;
use wasm_bindgen::{prelude::*, JsCast};
use wasm_timer::UNIX_EPOCH;

macro_rules! log {
( $( $t:tt )* ) => {
Expand All @@ -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...");
}

Expand All @@ -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()
Expand All @@ -85,6 +87,15 @@ 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 {
Expand All @@ -105,12 +116,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:
Expand Down
Loading

0 comments on commit bfebab1

Please sign in to comment.