Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite69400a

Browse files
committed
feat: add support for wasm
Adds support for compiling to WASM environments that provide JS viawasm-bindgen. Because there's no standardized socket API the caller mustprovide a connection that implements AsyncRead/AsyncWrite toconnect_raw.
1 parent762f67f commite69400a

File tree

5 files changed

+44
-11
lines changed

5 files changed

+44
-11
lines changed

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[workspace]
2+
resolver ="2"
23
members = [
34
"codegen",
45
"postgres",

‎postgres-protocol/Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ memchr = "2.0"
1919
rand ="0.8"
2020
sha2 ="0.10"
2121
stringprep ="0.1"
22+
23+
[target.'cfg(target_arch="wasm32")'.dependencies]
24+
getrandom = {version ="0.2.9",features = ["js"] }

‎tokio-postgres/Cargo.toml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ pin-project-lite = "0.2"
5555
phf ="0.11"
5656
postgres-protocol = {version ="0.6.5",path ="../postgres-protocol" }
5757
postgres-types = {version ="0.2.4",path ="../postgres-types" }
58+
tokio = {version ="1.27",default-features =false }
59+
tokio-util = {version ="0.7",default-features =false }
60+
61+
[target.'cfg(target_arch="wasm32")'.dependencies]
62+
tokio = {version ="1.27",features = ["io-util"],default-features =false }
63+
tokio-util = {version ="0.7",features = ["codec"],default-features =false }
64+
65+
[target.'cfg(not(target_arch="wasm32"))'.dependencies]
5866
socket2 = {version ="0.5",features = ["all"] }
5967
tokio = {version ="1.27",features = ["io-util"] }
6068
tokio-util = {version ="0.7",features = ["codec"] }

‎tokio-postgres/src/config.rs‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#[cfg(feature ="runtime")]
44
usecrate::connect::connect;
55
usecrate::connect_raw::connect_raw;
6+
#[cfg(not(target_arch ="wasm32"))]
67
usecrate::keepalive::KeepaliveConfig;
78
#[cfg(feature ="runtime")]
89
usecrate::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
implConfig{
180182
/// Creates a new configuration.
181183
pubfnnew() ->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
pubfnkeepalives_idle(&mutself,keepalives_idle:Duration) ->&mutConfig{
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
pubfnget_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
pubfnkeepalives_interval(&mutself,keepalives_interval:Duration) ->&mutConfig{
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
pubfnget_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
pubfnkeepalives_retries(&mutself,keepalives_retries:u32) ->&mutConfig{
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
pubfnget_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(timeoutasu64));
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_idleasu64));
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_intervalasu64));
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+
letmut 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‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ mod copy_in;
163163
mod copy_out;
164164
pubmod 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)

[8]ページ先頭

©2009-2025 Movatter.jp