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 Jul 27, 2023
1 parent 56c2f4b commit c2a5b56
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 217 deletions.
2 changes: 2 additions & 0 deletions rust_dev_preview/webassembly/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,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
29 changes: 17 additions & 12 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_credential_types::{provider::ProvideCredentials, Credentials};
use aws_sdk_lambda::{config::Region, meta::PKG_VERSION, Client};
use aws_smithy_async::rt::sleep::{AsyncSleep, Sleep};
use aws_smithy_async::time::TimeSource;
use aws_smithy_client::erase::DynConnector;
use aws_smithy_http::{body::SdkBody, 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,19 +61,17 @@ 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(DynConnector::new(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 @@ -88,6 +90,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 @@ -108,12 +119,6 @@ fn static_credential_provider() -> impl ProvideCredentials {
)
}

fn browser_credentials_cache() -> CredentialsCache {
CredentialsCache::lazy_builder()
.sleep(std::sync::Arc::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 c2a5b56

Please sign in to comment.