Skip to content

Commit 852869d

Browse files
authored
Merge pull request sfackler#1040 from zebp/feat/wasm-support
feat: add support for wasm
2 parents 762f67f + 635bac4 commit 852869d

File tree

5 files changed

+74
-13
lines changed

5 files changed

+74
-13
lines changed

.github/workflows/ci.yml

+33-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- uses: actions/checkout@v3
2121
- uses: sfackler/actions/rustup@master
2222
- uses: sfackler/actions/rustfmt@master
23-
23+
2424
clippy:
2525
name: clippy
2626
runs-on: ubuntu-latest
@@ -47,6 +47,38 @@ jobs:
4747
key: clippy-target-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}y
4848
- run: cargo clippy --all --all-targets
4949

50+
check-wasm32:
51+
name: check-wasm32
52+
runs-on: ubuntu-latest
53+
steps:
54+
- uses: actions/checkout@v3
55+
- uses: sfackler/actions/rustup@master
56+
- run: echo "version=$(rustc --version)" >> $GITHUB_OUTPUT
57+
id: rust-version
58+
- run: rustup target add wasm32-unknown-unknown
59+
- uses: actions/cache@v3
60+
with:
61+
path: ~/.cargo/registry/index
62+
key: index-${{ runner.os }}-${{ github.run_number }}
63+
restore-keys: |
64+
index-${{ runner.os }}-
65+
- run: cargo generate-lockfile
66+
- uses: actions/cache@v3
67+
with:
68+
path: ~/.cargo/registry/cache
69+
key: registry-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}
70+
- run: cargo fetch
71+
- uses: actions/cache@v3
72+
with:
73+
path: target
74+
key: clippy-target-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}y
75+
- run: |
76+
# Hack: wasm support currently relies on not having tokio with features like socket enabled. With resolver 1
77+
# dev dependencies can add unwanted dependencies to the build, so we'll hackily disable them for this check.
78+
79+
sed -i 's/\[dev-dependencies]/[ignore-dependencies]/g' ./tokio-postgres/Cargo.toml
80+
cargo check --target wasm32-unknown-unknown --manifest-path tokio-postgres/Cargo.toml --no-default-features --features js
81+
5082
test:
5183
name: test
5284
runs-on: ubuntu-latest

postgres-protocol/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ license = "MIT/Apache-2.0"
88
repository = "https://github.com/sfackler/rust-postgres"
99
readme = "../README.md"
1010

11+
[features]
12+
default = []
13+
js = ["getrandom/js"]
14+
1115
[dependencies]
1216
base64 = "0.21"
1317
byteorder = "1.0"
@@ -19,3 +23,4 @@ memchr = "2.0"
1923
rand = "0.8"
2024
sha2 = "0.10"
2125
stringprep = "0.1"
26+
getrandom = { version = "0.2", optional = true }

tokio-postgres/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ with-uuid-0_8 = ["postgres-types/with-uuid-0_8"]
4040
with-uuid-1 = ["postgres-types/with-uuid-1"]
4141
with-time-0_2 = ["postgres-types/with-time-0_2"]
4242
with-time-0_3 = ["postgres-types/with-time-0_3"]
43+
js = ["postgres-protocol/js"]
4344

4445
[dependencies]
4546
async-trait = "0.1"
@@ -55,10 +56,12 @@ pin-project-lite = "0.2"
5556
phf = "0.11"
5657
postgres-protocol = { version = "0.6.5", path = "../postgres-protocol" }
5758
postgres-types = { version = "0.2.4", path = "../postgres-types" }
58-
socket2 = { version = "0.5", features = ["all"] }
5959
tokio = { version = "1.27", features = ["io-util"] }
6060
tokio-util = { version = "0.7", features = ["codec"] }
6161

62+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
63+
socket2 = { version = "0.5", features = ["all"] }
64+
6265
[dev-dependencies]
6366
futures-executor = "0.3"
6467
criterion = "0.5"

tokio-postgres/src/config.rs

+31-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#[cfg(feature = "runtime")]
44
use crate::connect::connect;
55
use crate::connect_raw::connect_raw;
6+
#[cfg(not(target_arch = "wasm32"))]
67
use crate::keepalive::KeepaliveConfig;
78
#[cfg(feature = "runtime")]
89
use crate::tls::MakeTlsConnect;
@@ -165,6 +166,7 @@ pub struct Config {
165166
pub(crate) connect_timeout: Option<Duration>,
166167
pub(crate) tcp_user_timeout: Option<Duration>,
167168
pub(crate) keepalives: bool,
169+
#[cfg(not(target_arch = "wasm32"))]
168170
pub(crate) keepalive_config: KeepaliveConfig,
169171
pub(crate) target_session_attrs: TargetSessionAttrs,
170172
pub(crate) channel_binding: ChannelBinding,
@@ -179,11 +181,6 @@ impl Default for Config {
179181
impl Config {
180182
/// Creates a new configuration.
181183
pub fn new() -> Config {
182-
let keepalive_config = KeepaliveConfig {
183-
idle: Duration::from_secs(2 * 60 * 60),
184-
interval: None,
185-
retries: None,
186-
};
187184
Config {
188185
user: None,
189186
password: None,
@@ -196,7 +193,12 @@ impl Config {
196193
connect_timeout: None,
197194
tcp_user_timeout: None,
198195
keepalives: true,
199-
keepalive_config,
196+
#[cfg(not(target_arch = "wasm32"))]
197+
keepalive_config: KeepaliveConfig {
198+
idle: Duration::from_secs(2 * 60 * 60),
199+
interval: None,
200+
retries: None,
201+
},
200202
target_session_attrs: TargetSessionAttrs::Any,
201203
channel_binding: ChannelBinding::Prefer,
202204
}
@@ -377,13 +379,15 @@ impl Config {
377379
/// Sets the amount of idle time before a keepalive packet is sent on the connection.
378380
///
379381
/// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled. Defaults to 2 hours.
382+
#[cfg(not(target_arch = "wasm32"))]
380383
pub fn keepalives_idle(&mut self, keepalives_idle: Duration) -> &mut Config {
381384
self.keepalive_config.idle = keepalives_idle;
382385
self
383386
}
384387

385388
/// Gets the configured amount of idle time before a keepalive packet will
386389
/// be sent on the connection.
390+
#[cfg(not(target_arch = "wasm32"))]
387391
pub fn get_keepalives_idle(&self) -> Duration {
388392
self.keepalive_config.idle
389393
}
@@ -392,25 +396,29 @@ impl Config {
392396
/// On Windows, this sets the value of the tcp_keepalive struct’s keepaliveinterval field.
393397
///
394398
/// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled.
399+
#[cfg(not(target_arch = "wasm32"))]
395400
pub fn keepalives_interval(&mut self, keepalives_interval: Duration) -> &mut Config {
396401
self.keepalive_config.interval = Some(keepalives_interval);
397402
self
398403
}
399404

400405
/// Gets the time interval between TCP keepalive probes.
406+
#[cfg(not(target_arch = "wasm32"))]
401407
pub fn get_keepalives_interval(&self) -> Option<Duration> {
402408
self.keepalive_config.interval
403409
}
404410

405411
/// Sets the maximum number of TCP keepalive probes that will be sent before dropping a connection.
406412
///
407413
/// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled.
414+
#[cfg(not(target_arch = "wasm32"))]
408415
pub fn keepalives_retries(&mut self, keepalives_retries: u32) -> &mut Config {
409416
self.keepalive_config.retries = Some(keepalives_retries);
410417
self
411418
}
412419

413420
/// Gets the maximum number of TCP keepalive probes that will be sent before dropping a connection.
421+
#[cfg(not(target_arch = "wasm32"))]
414422
pub fn get_keepalives_retries(&self) -> Option<u32> {
415423
self.keepalive_config.retries
416424
}
@@ -503,12 +511,14 @@ impl Config {
503511
self.tcp_user_timeout(Duration::from_secs(timeout as u64));
504512
}
505513
}
514+
#[cfg(not(target_arch = "wasm32"))]
506515
"keepalives" => {
507516
let keepalives = value
508517
.parse::<u64>()
509518
.map_err(|_| Error::config_parse(Box::new(InvalidValue("keepalives"))))?;
510519
self.keepalives(keepalives != 0);
511520
}
521+
#[cfg(not(target_arch = "wasm32"))]
512522
"keepalives_idle" => {
513523
let keepalives_idle = value
514524
.parse::<i64>()
@@ -517,6 +527,7 @@ impl Config {
517527
self.keepalives_idle(Duration::from_secs(keepalives_idle as u64));
518528
}
519529
}
530+
#[cfg(not(target_arch = "wasm32"))]
520531
"keepalives_interval" => {
521532
let keepalives_interval = value.parse::<i64>().map_err(|_| {
522533
Error::config_parse(Box::new(InvalidValue("keepalives_interval")))
@@ -525,6 +536,7 @@ impl Config {
525536
self.keepalives_interval(Duration::from_secs(keepalives_interval as u64));
526537
}
527538
}
539+
#[cfg(not(target_arch = "wasm32"))]
528540
"keepalives_retries" => {
529541
let keepalives_retries = value.parse::<u32>().map_err(|_| {
530542
Error::config_parse(Box::new(InvalidValue("keepalives_retries")))
@@ -614,7 +626,8 @@ impl fmt::Debug for Config {
614626
}
615627
}
616628

617-
f.debug_struct("Config")
629+
let mut config_dbg = &mut f.debug_struct("Config");
630+
config_dbg = config_dbg
618631
.field("user", &self.user)
619632
.field("password", &self.password.as_ref().map(|_| Redaction {}))
620633
.field("dbname", &self.dbname)
@@ -625,10 +638,17 @@ impl fmt::Debug for Config {
625638
.field("port", &self.port)
626639
.field("connect_timeout", &self.connect_timeout)
627640
.field("tcp_user_timeout", &self.tcp_user_timeout)
628-
.field("keepalives", &self.keepalives)
629-
.field("keepalives_idle", &self.keepalive_config.idle)
630-
.field("keepalives_interval", &self.keepalive_config.interval)
631-
.field("keepalives_retries", &self.keepalive_config.retries)
641+
.field("keepalives", &self.keepalives);
642+
643+
#[cfg(not(target_arch = "wasm32"))]
644+
{
645+
config_dbg = config_dbg
646+
.field("keepalives_idle", &self.keepalive_config.idle)
647+
.field("keepalives_interval", &self.keepalive_config.interval)
648+
.field("keepalives_retries", &self.keepalive_config.retries);
649+
}
650+
651+
config_dbg
632652
.field("target_session_attrs", &self.target_session_attrs)
633653
.field("channel_binding", &self.channel_binding)
634654
.finish()

tokio-postgres/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ mod copy_in;
163163
mod copy_out;
164164
pub mod error;
165165
mod generic_client;
166+
#[cfg(not(target_arch = "wasm32"))]
166167
mod keepalive;
167168
mod maybe_tls_stream;
168169
mod portal;

0 commit comments

Comments
 (0)