Skip to content

Commit 2325802

Browse files
committed
Merge remote-tracking branch 'dbw9580/fix-wasm-time'
2 parents 1742f17 + 3bc307a commit 2325802

File tree

5 files changed

+66
-9
lines changed

5 files changed

+66
-9
lines changed

.cargo/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.wasm32-unknown-unknown]
2+
runner = "wasm-bindgen-test-runner"

.github/workflows/ci.yml

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,50 @@ jobs:
1616
- beta
1717
- nightly
1818
- '1.46.0'
19-
features:
19+
target:
2020
-
21-
- derive
22-
- hmac-sha1
21+
features:
22+
- js
23+
- js,derive
24+
- js,hmac-sha1
25+
include:
26+
- toolchain: stable
27+
features: js
28+
target: wasm32-unknown-unknown
2329
steps:
2430
- uses: actions/checkout@v2
2531
- name: Install Rust toolchain
2632
uses: actions-rs/toolchain@v1
2733
with:
2834
toolchain: ${{ matrix.toolchain }}
35+
target: ${{ matrix.target }}
2936
profile: minimal
3037
override: true
3138
id: toolchain
3239
- uses: Swatinem/rust-cache@v1
40+
with:
41+
key: ${{ matrix.target }}
42+
- name: Install `wasm-bindgen-test-runner`
43+
if: matrix.target == 'wasm32-unknown-unknown'
44+
run: |
45+
VER=0.2.78
46+
NAME="wasm-bindgen-$VER-x86_64-unknown-linux-musl"
47+
DIGEST=14f1b0ef9225370f0d270efbdbbfe2cf5eb191d57b8eec14ade69c98c71e226f
48+
curl -fLOsS "https://github.com/rustwasm/wasm-bindgen/releases/download/$VER/$NAME.tar.gz"
49+
sha256sum --check --quiet <<< "$DIGEST $NAME.tar.gz"
50+
tar -xzf "$NAME.tar.gz" "$NAME/wasm-bindgen-test-runner"
51+
mv "$NAME/wasm-bindgen-test-runner" /usr/local/bin/
3352
- run: echo 'RUSTFLAGS=--allow unknown_lints' >> $GITHUB_ENV
3453
if: matrix.toolchain == '1.46.0'
54+
- run: echo 'CARGO_BUILD_TARGET=${{ matrix.target }}' >> $GITHUB_ENV
55+
if: matrix.target != ''
3556
- name: Build `oauth1-request`
3657
uses: actions-rs/cargo@v1
3758
with:
3859
command: build
3960
args: --verbose --tests --manifest-path oauth1-request/Cargo.toml --features=${{ matrix.features }}
4061
- name: Build `examples`
41-
if: ${{ matrix.toolchain != '1.46.0' }}
62+
if: ${{ matrix.target == '' && matrix.toolchain != '1.46.0' }}
4263
uses: actions-rs/cargo@v1
4364
with:
4465
command: build

oauth1-request/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,22 @@ either = { version = "1.2", optional = true }
3030
hmac = { version = "0.11", optional = true }
3131
sha-1 = { version = "0.9", optional = true }
3232

33+
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
34+
# `js` feature
35+
js-sys = { version = "0.3", optional = true }
36+
3337
[dev-dependencies]
3438
# Trick to make `proc-macro-crate` work in doctests.
3539
oauth1-request = { version = "0.5", path = "", default-features = false }
3640
version-sync = "0.9"
3741

42+
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dev-dependencies]
43+
getrandom = { version = "0.2", features = ["js"] }
44+
wasm-bindgen-test = "0.3"
45+
3846
[features]
3947
default = ["derive", "hmac-sha1"]
4048
derive = ["oauth1-request-derive"]
4149
hmac-sha1 = ["hmac", "sha-1"]
50+
js = ["js-sys"]
4251
serde = ["oauth-credentials/serde"]

oauth1-request/src/serializer/auth.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::fmt::{Display, Write};
44
use std::num::NonZeroU64;
55
use std::str;
6-
use std::time::{SystemTime, UNIX_EPOCH};
76

87
use rand::prelude::*;
98

@@ -235,10 +234,7 @@ impl<'a, SM: SignatureMethod> Serializer for Authorizer<'a, SM> {
235234
let t = if let Some(t) = self.options.timestamp {
236235
t.get()
237236
} else {
238-
match SystemTime::now().duration_since(UNIX_EPOCH) {
239-
Ok(d) => d.as_secs(),
240-
Err(_) => 1,
241-
}
237+
get_current_timestamp()
242238
};
243239
append_to_header!(self, encoded timestamp, t);
244240
}
@@ -277,6 +273,21 @@ impl<'a, SM: SignatureMethod> Serializer for Authorizer<'a, SM> {
277273
}
278274
}
279275

276+
fn get_current_timestamp() -> u64 {
277+
cfg_if::cfg_if! {
278+
// `std::time::SystemTime::now` is not supported and panics on `wasm32-unknown-unknown` target
279+
if #[cfg(all(feature = "js", target_arch = "wasm32", target_os = "unknown"))] {
280+
(js_sys::Date::now() / 1000.0) as u64
281+
} else {
282+
use std::time::{SystemTime, UNIX_EPOCH};
283+
match SystemTime::now().duration_since(UNIX_EPOCH) {
284+
Ok(d) => d.as_secs(),
285+
Err(_) => 1,
286+
}
287+
}
288+
}
289+
}
290+
280291
// This is worth 72 bits of entropy. The nonce is required to be unique across all requests with
281292
// the same timestamp, client and token. Even if you generate the nonce one million times a second
282293
// (which is unlikely unless you are DoS-ing the server or something), the expected time it takes

oauth1-request/tests/pull_9.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Test for <https://github.com/tesaguri/oauth1-request-rs/pull/9>.
2+
3+
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
4+
use wasm_bindgen_test::wasm_bindgen_test as test;
5+
6+
#[test]
7+
fn pull_9() {
8+
let _ = oauth1_request::get(
9+
"",
10+
&(),
11+
&oauth_credentials::Token::from_parts("", "", "", ""),
12+
oauth1_request::HmacSha1,
13+
);
14+
}

0 commit comments

Comments
 (0)