rwebtransport 0.2.2 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/Cargo.lock +523 -140
  2. package/Cargo.toml +5 -5
  3. package/README.md +8 -4
  4. package/crates/echo-server/src/bin/gen-test-cert.rs +1 -1
  5. package/crates/native/Cargo.toml +2 -0
  6. package/crates/native/src/config.rs +69 -8
  7. package/crates/native/src/driver.rs +253 -90
  8. package/crates/native/src/lib.rs +80 -57
  9. package/crates/native/src/parser_memory.rs +203 -0
  10. package/crates/native/src/server.rs +215 -183
  11. package/crates/native/src/session.rs +1341 -243
  12. package/crates/native/src/test_support.rs +119 -0
  13. package/crates/wtcore/Cargo.toml +1 -0
  14. package/crates/wtcore/src/h3.rs +250 -46
  15. package/dist/index.js +218 -75
  16. package/dist/index.js.map +7 -1
  17. package/dist/index.mjs +214 -69
  18. package/dist/index.mjs.map +7 -1
  19. package/dist/native.d.ts +10 -4
  20. package/dist/server.d.ts +1 -0
  21. package/package.json +84 -78
  22. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  24. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  25. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  26. package/scripts/build-linux-prebuilt.sh +46 -0
  27. package/scripts/build-ts.js +41 -0
  28. package/scripts/build.js +10 -3
  29. package/vendor/quiche/AGENTS.md +10 -0
  30. package/vendor/quiche/Cargo.lock +239 -248
  31. package/vendor/quiche/Cargo.toml +23 -10
  32. package/vendor/quiche/examples/Makefile +4 -4
  33. package/vendor/quiche/examples/client.rs +10 -28
  34. package/vendor/quiche/examples/http3-client.rs +20 -34
  35. package/vendor/quiche/examples/http3-server.rs +47 -78
  36. package/vendor/quiche/examples/server.rs +19 -49
  37. package/vendor/quiche/include/quiche.h +35 -4
  38. package/vendor/quiche/src/build.rs +17 -0
  39. package/vendor/quiche/src/cid.rs +129 -61
  40. package/vendor/quiche/src/crypto/boringssl.rs +98 -86
  41. package/vendor/quiche/src/crypto/mod.rs +64 -77
  42. package/vendor/quiche/src/dgram.rs +2 -3
  43. package/vendor/quiche/src/error.rs +22 -20
  44. package/vendor/quiche/src/ffi.rs +558 -280
  45. package/vendor/quiche/src/flowcontrol.rs +0 -7
  46. package/vendor/quiche/src/frame.rs +134 -135
  47. package/vendor/quiche/src/h3/ffi.rs +73 -60
  48. package/vendor/quiche/src/h3/frame.rs +70 -90
  49. package/vendor/quiche/src/h3/mod.rs +1024 -593
  50. package/vendor/quiche/src/h3/qpack/decoder.rs +248 -34
  51. package/vendor/quiche/src/h3/qpack/dynamic_table.rs +859 -0
  52. package/vendor/quiche/src/h3/qpack/encoder.rs +14 -17
  53. package/vendor/quiche/src/h3/qpack/mod.rs +48 -0
  54. package/vendor/quiche/src/h3/qpack/static_table.rs +86 -73
  55. package/vendor/quiche/src/h3/stream.rs +1009 -194
  56. package/vendor/quiche/src/lib.rs +1361 -1195
  57. package/vendor/quiche/src/minmax.rs +2 -6
  58. package/vendor/quiche/src/packet.rs +420 -552
  59. package/vendor/quiche/src/path.rs +202 -72
  60. package/vendor/quiche/src/pmtud.rs +18 -16
  61. package/vendor/quiche/src/ranges.rs +184 -82
  62. package/vendor/quiche/src/recovery/AGENTS.md +5 -2
  63. package/vendor/quiche/src/recovery/bandwidth.rs +59 -40
  64. package/vendor/quiche/src/recovery/bytes_in_flight.rs +2 -2
  65. package/vendor/quiche/src/recovery/congestion/cubic.rs +38 -47
  66. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +54 -60
  67. package/vendor/quiche/src/recovery/congestion/hystart.rs +6 -10
  68. package/vendor/quiche/src/recovery/congestion/mod.rs +110 -42
  69. package/vendor/quiche/src/recovery/congestion/prr.rs +4 -1
  70. package/vendor/quiche/src/recovery/congestion/recovery.rs +91 -95
  71. package/vendor/quiche/src/recovery/congestion/reno.rs +17 -12
  72. package/vendor/quiche/src/recovery/congestion/test_sender.rs +8 -27
  73. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +159 -247
  74. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +9 -14
  75. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +18 -15
  76. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +43 -28
  77. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +349 -76
  78. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +147 -195
  79. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +29 -27
  80. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min.rs +180 -0
  81. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min_tests.rs +221 -0
  82. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm.rs +395 -0
  83. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm_tests.rs +473 -0
  84. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/mod.rs +175 -0
  85. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +33 -26
  86. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +107 -60
  87. package/vendor/quiche/src/recovery/gcongestion/mod.rs +64 -11
  88. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +34 -25
  89. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +81 -86
  90. package/vendor/quiche/src/recovery/mod.rs +414 -173
  91. package/vendor/quiche/src/recovery/rtt.rs +8 -5
  92. package/vendor/quiche/src/stream/mod.rs +864 -133
  93. package/vendor/quiche/src/stream/recv_buf.rs +476 -172
  94. package/vendor/quiche/src/stream/send_buf.rs +343 -81
  95. package/vendor/quiche/src/test_utils.rs +78 -64
  96. package/vendor/quiche/src/tests.rs +2559 -1335
  97. package/vendor/quiche/src/tls/boringssl.rs +60 -63
  98. package/vendor/quiche/src/tls/mod.rs +146 -193
  99. package/vendor/quiche/src/transport_params.rs +87 -103
@@ -25,8 +25,11 @@
25
25
 
26
26
  mod config;
27
27
  mod driver;
28
+ mod parser_memory;
28
29
  mod server;
29
30
  mod session;
31
+ #[cfg(test)]
32
+ mod test_support;
30
33
  mod tls;
31
34
 
32
35
  use std::net::IpAddr;
@@ -142,52 +145,28 @@ struct ParsedUrl {
142
145
  /// Parse a WebTransport URL WITHOUT resolving DNS (resolution is deferred to the
143
146
  /// driver thread so it never blocks the JS event loop).
144
147
  fn parse_url(url: &str) -> Result<ParsedUrl, String> {
145
- let rest = url
146
- .strip_prefix("https://")
147
- .ok_or_else(|| "WebTransport URL must use the https:// scheme".to_string())?;
148
-
149
- let (authority, path) = match rest.find('/') {
150
- Some(i) => (&rest[..i], rest[i..].to_string()),
151
- None => (rest, "/".to_string()),
152
- };
153
- if authority.is_empty() {
154
- return Err("WebTransport URL has an empty authority".to_string());
148
+ let parsed = url::Url::parse(url).map_err(|e| format!("invalid WebTransport URL: {e}"))?;
149
+ if parsed.scheme() != "https"
150
+ || parsed.fragment().is_some()
151
+ || !parsed.username().is_empty()
152
+ || parsed.password().is_some()
153
+ {
154
+ return Err("WebTransport URL must use HTTPS without credentials or a fragment".into());
155
155
  }
156
-
157
- // Split host and port, handling IPv6 literals.
158
- let (host, port): (String, u16) = if let Some(hstr) = authority.strip_prefix('[') {
159
- let end = hstr
160
- .find(']')
161
- .ok_or_else(|| "malformed IPv6 authority".to_string())?;
162
- let host = hstr[..end].to_string();
163
- let after = &hstr[end + 1..];
164
- let port = after
165
- .strip_prefix(':')
166
- .map(|p| p.parse::<u16>().map_err(|_| "invalid port".to_string()))
167
- .transpose()?
168
- .unwrap_or(443);
169
- (host, port)
170
- } else if let Some(i) = authority.rfind(':') {
171
- let host = authority[..i].to_string();
172
- let port = authority[i + 1..]
173
- .parse::<u16>()
174
- .map_err(|_| "invalid port".to_string())?;
175
- (host, port)
176
- } else {
177
- (authority.to_string(), 443)
178
- };
179
-
180
- let sni = if host.parse::<IpAddr>().is_ok() {
181
- None
182
- } else {
183
- Some(host.clone())
156
+ let host = match parsed.host().ok_or("WebTransport URL has no host")? {
157
+ url::Host::Domain(name) => name.to_string(),
158
+ url::Host::Ipv4(ip) => ip.to_string(),
159
+ url::Host::Ipv6(ip) => ip.to_string(),
184
160
  };
185
-
161
+ let port = parsed.port_or_known_default().ok_or("invalid port")?;
162
+ let authority = parsed[url::Position::BeforeHost..url::Position::AfterPort].to_string();
163
+ let path = parsed[url::Position::BeforePath..url::Position::AfterQuery].to_string();
164
+ let sni = host.parse::<IpAddr>().is_err().then(|| host.clone());
186
165
  Ok(ParsedUrl {
187
166
  host,
188
167
  port,
189
168
  sni,
190
- authority: authority.to_string(),
169
+ authority,
191
170
  path,
192
171
  })
193
172
  }
@@ -288,6 +267,7 @@ fn connect(mut cx: FunctionContext) -> JsResult<JsBox<SessionHandle>> {
288
267
  };
289
268
 
290
269
  let params = ClientConfigParams {
270
+ expected_ip: parsed.host.parse().ok(),
291
271
  verify,
292
272
  ..ClientConfigParams::default()
293
273
  };
@@ -445,7 +425,11 @@ fn export_keying_material(mut cx: FunctionContext) -> JsResult<JsUndefined> {
445
425
  let request_id = to_request_id(cx.argument::<JsNumber>(1)?.value(&mut cx));
446
426
  let label = cx.argument::<JsTypedArray<u8>>(2)?.as_slice(&cx).to_vec();
447
427
  let context = cx.argument::<JsTypedArray<u8>>(3)?.as_slice(&cx).to_vec();
448
- let length = cx.argument::<JsNumber>(4)?.value(&mut cx) as u32;
428
+ let length = cx.argument::<JsNumber>(4)?.value(&mut cx);
429
+ if !length.is_finite() || length.fract() != 0.0 || !(1.0..=4096.0).contains(&length) {
430
+ return cx.throw_range_error("outputLength must be an integer between 1 and 4096");
431
+ }
432
+ let length = length as u32;
449
433
  handle.send(Command::ExportKeyingMaterial {
450
434
  request_id,
451
435
  label,
@@ -467,7 +451,6 @@ use server::{ServerCommand, ServerEventSink, ServerMsg, ServerSetup};
467
451
  struct ServerHandle {
468
452
  tx: Mutex<Option<Sender<ServerCommand>>>,
469
453
  waker: Arc<mio::Waker>,
470
- shared: Arc<DriverShared>,
471
454
  thread: Mutex<Option<std::thread::JoinHandle<()>>>,
472
455
  }
473
456
 
@@ -617,7 +600,6 @@ fn server_listen(mut cx: FunctionContext) -> JsResult<JsBox<ServerHandle>> {
617
600
  Ok(cx.boxed(ServerHandle {
618
601
  tx: Mutex::new(Some(tx)),
619
602
  waker,
620
- shared,
621
603
  thread: Mutex::new(Some(thread)),
622
604
  }))
623
605
  }
@@ -702,12 +684,6 @@ fn server_send_datagram(mut cx: FunctionContext) -> JsResult<JsUndefined> {
702
684
  Ok(cx.undefined())
703
685
  }
704
686
 
705
- fn server_max_datagram_size(mut cx: FunctionContext) -> JsResult<JsNumber> {
706
- let handle = cx.argument::<JsBox<ServerHandle>>(0)?;
707
- let n = handle.shared.max_datagram_size.load(Ordering::Relaxed);
708
- Ok(cx.number(n as f64))
709
- }
710
-
711
687
  fn server_close_session(mut cx: FunctionContext) -> JsResult<JsUndefined> {
712
688
  let handle = cx.argument::<JsBox<ServerHandle>>(0)?;
713
689
  let session = to_stream_id(cx.argument::<JsNumber>(1)?.value(&mut cx));
@@ -745,7 +721,11 @@ fn server_export_keying_material(mut cx: FunctionContext) -> JsResult<JsUndefine
745
721
  let request_id = to_request_id(cx.argument::<JsNumber>(2)?.value(&mut cx));
746
722
  let label = cx.argument::<JsTypedArray<u8>>(3)?.as_slice(&cx).to_vec();
747
723
  let context = cx.argument::<JsTypedArray<u8>>(4)?.as_slice(&cx).to_vec();
748
- let length = cx.argument::<JsNumber>(5)?.value(&mut cx) as u32;
724
+ let length = cx.argument::<JsNumber>(5)?.value(&mut cx);
725
+ if !length.is_finite() || length.fract() != 0.0 || !(1.0..=4096.0).contains(&length) {
726
+ return cx.throw_range_error("outputLength must be an integer between 1 and 4096");
727
+ }
728
+ let length = length as u32;
749
729
  handle.send(ServerCommand::ExportKeyingMaterial {
750
730
  session,
751
731
  request_id,
@@ -898,6 +878,10 @@ fn ev_to_js<'a>(cx: &mut TaskContext<'a>, ev: &Ev) -> JsResult<'a, JsObject> {
898
878
  set_str(cx, &obj, "type", "datagram")?;
899
879
  set_buf(cx, &obj, "data", data)?;
900
880
  }
881
+ Ev::DatagramSize(size) => {
882
+ set_str(cx, &obj, "type", "datagramSize")?;
883
+ set_num(cx, &obj, "size", *size as f64)?;
884
+ }
901
885
  Ev::IncomingStream { id, bidi } => {
902
886
  set_str(cx, &obj, "type", "stream")?;
903
887
  set_num(cx, &obj, "streamId", *id as f64)?;
@@ -927,9 +911,13 @@ fn ev_to_js<'a>(cx: &mut TaskContext<'a>, ev: &Ev) -> JsResult<'a, JsObject> {
927
911
  set_num(cx, &obj, "requestId", *request_id as f64)?;
928
912
  set_num(cx, &obj, "streamId", *id as f64)?;
929
913
  }
930
- Ev::WriteAck { request_id } => {
914
+ Ev::WriteAck {
915
+ request_id,
916
+ accepted,
917
+ } => {
931
918
  set_str(cx, &obj, "type", "writeAck")?;
932
919
  set_num(cx, &obj, "requestId", *request_id as f64)?;
920
+ set_bool(cx, &obj, "accepted", *accepted)?;
933
921
  }
934
922
  Ev::DatagramAck { request_id, sent } => {
935
923
  set_str(cx, &obj, "type", "datagramAck")?;
@@ -1025,7 +1013,6 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
1025
1013
  cx.export_function("serverStopSending", server_stop_sending)?;
1026
1014
  cx.export_function("serverSetPaused", server_set_paused)?;
1027
1015
  cx.export_function("serverSendDatagram", server_send_datagram)?;
1028
- cx.export_function("serverMaxDatagramSize", server_max_datagram_size)?;
1029
1016
  cx.export_function("serverCloseSession", server_close_session)?;
1030
1017
  cx.export_function("serverDrain", server_drain)?;
1031
1018
  cx.export_function("serverGetStats", server_get_stats)?;
@@ -1056,21 +1043,57 @@ mod tests {
1056
1043
  // must wake the driver EXACTLY when the decrement crosses back below the cap: not while still at or
1057
1044
  // above it (still back-pressured), and not when already below it (a spurious wake). Getting this
1058
1045
  // wrong is the stranded-terminal-frame hang.
1046
+ #[test]
1047
+ fn urls_are_canonicalized_without_credentials_or_fragments() {
1048
+ let parsed = super::parse_url("https://example.com:8443?query=1").unwrap();
1049
+ assert_eq!(parsed.host, "example.com");
1050
+ assert_eq!(parsed.port, 8443);
1051
+ assert_eq!(parsed.path, "/?query=1");
1052
+ let parsed = super::parse_url("https://[::1]:8443/a?b=2").unwrap();
1053
+ assert_eq!(parsed.host, "::1");
1054
+ assert_eq!(parsed.authority, "[::1]:8443");
1055
+ assert_eq!(parsed.path, "/a?b=2");
1056
+ assert!(parsed.sni.is_none());
1057
+ for url in [
1058
+ "http://example.com/",
1059
+ "https://user:password@example.com/",
1060
+ "https://example.com/#",
1061
+ "https://[::1]garbage/",
1062
+ ] {
1063
+ assert!(super::parse_url(url).is_err());
1064
+ }
1065
+ }
1066
+
1059
1067
  #[test]
1060
1068
  fn release_wakes_only_when_the_decrement_clears_backpressure() {
1061
1069
  let mut poll = mio::Poll::new().expect("poll");
1062
1070
  let waker = mio::Waker::new(poll.registry(), driver::WAKE_TOKEN).expect("waker");
1063
1071
 
1064
1072
  // Already below the cap: not back-pressured, so no wake.
1065
- release_inflight_batch(&shared_with_inflight(driver::MAX_INFLIGHT_BATCHES - 1), &waker);
1066
- assert!(!waker_fired(&mut poll), "no wake when already below the cap");
1073
+ release_inflight_batch(
1074
+ &shared_with_inflight(driver::MAX_INFLIGHT_BATCHES - 1),
1075
+ &waker,
1076
+ );
1077
+ assert!(
1078
+ !waker_fired(&mut poll),
1079
+ "no wake when already below the cap"
1080
+ );
1067
1081
 
1068
1082
  // Still at/above the cap after the decrement: still back-pressured, so no wake.
1069
- release_inflight_batch(&shared_with_inflight(driver::MAX_INFLIGHT_BATCHES + 1), &waker);
1070
- assert!(!waker_fired(&mut poll), "no wake while still at or above the cap");
1083
+ release_inflight_batch(
1084
+ &shared_with_inflight(driver::MAX_INFLIGHT_BATCHES + 1),
1085
+ &waker,
1086
+ );
1087
+ assert!(
1088
+ !waker_fired(&mut poll),
1089
+ "no wake while still at or above the cap"
1090
+ );
1071
1091
 
1072
1092
  // Exactly at the cap: the decrement crosses below it, clearing back-pressure -> wake.
1073
1093
  release_inflight_batch(&shared_with_inflight(driver::MAX_INFLIGHT_BATCHES), &waker);
1074
- assert!(waker_fired(&mut poll), "wake exactly when back-pressure clears");
1094
+ assert!(
1095
+ waker_fired(&mut poll),
1096
+ "wake exactly when back-pressure clears"
1097
+ );
1075
1098
  }
1076
1099
  }
@@ -0,0 +1,203 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ //! Allocation accounting shared by every receive parser on a server.
3
+
4
+ use std::ops::Deref;
5
+ use std::sync::atomic::{AtomicUsize, Ordering};
6
+ use std::sync::Arc;
7
+
8
+ pub(crate) struct ParserMemory {
9
+ limit: usize,
10
+ used: AtomicUsize,
11
+ parent: Option<Arc<ParserMemory>>,
12
+ }
13
+
14
+ impl ParserMemory {
15
+ pub(crate) fn new(limit: usize) -> Arc<Self> {
16
+ Arc::new(Self {
17
+ limit,
18
+ used: AtomicUsize::new(0),
19
+ parent: None,
20
+ })
21
+ }
22
+
23
+ pub(crate) fn child(parent: Arc<Self>, limit: usize) -> Arc<Self> {
24
+ Arc::new(Self {
25
+ limit,
26
+ used: AtomicUsize::new(0),
27
+ parent: Some(parent),
28
+ })
29
+ }
30
+
31
+ #[allow(deprecated)] // Use the atomic API available on older Rust toolchains.
32
+ fn reserve(&self, bytes: usize) -> bool {
33
+ let reserved = self
34
+ .used
35
+ .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |used| {
36
+ used.checked_add(bytes).filter(|&next| next <= self.limit)
37
+ })
38
+ .is_ok();
39
+ if !reserved {
40
+ return false;
41
+ }
42
+ if let Some(parent) = &self.parent {
43
+ if !parent.reserve(bytes) {
44
+ self.used.fetch_sub(bytes, Ordering::Relaxed);
45
+ return false;
46
+ }
47
+ }
48
+ true
49
+ }
50
+
51
+ fn release(&self, bytes: usize) {
52
+ self.used.fetch_sub(bytes, Ordering::Relaxed);
53
+ if let Some(parent) = &self.parent {
54
+ parent.release(bytes);
55
+ }
56
+ }
57
+
58
+ #[cfg(test)]
59
+ pub(crate) fn used(&self) -> usize {
60
+ self.used.load(Ordering::Relaxed)
61
+ }
62
+ }
63
+
64
+ pub(crate) struct ParserBuffer {
65
+ data: Vec<u8>,
66
+ memory: Arc<ParserMemory>,
67
+ }
68
+
69
+ impl ParserBuffer {
70
+ pub(crate) fn new(memory: Arc<ParserMemory>) -> Self {
71
+ Self {
72
+ data: Vec::new(),
73
+ memory,
74
+ }
75
+ }
76
+
77
+ pub(crate) fn extend_from_slice(&mut self, bytes: &[u8]) -> Result<(), ()> {
78
+ let needed = self.data.len().checked_add(bytes.len()).ok_or(())?;
79
+ if needed > self.data.capacity() {
80
+ let capacity = needed.checked_next_power_of_two().ok_or(())?;
81
+ // Reserve the whole replacement before allocation: the old and new
82
+ // buffers coexist during growth, and both must fit the budget.
83
+ if !self.memory.reserve(capacity) {
84
+ return Err(());
85
+ }
86
+ let mut replacement = Vec::new();
87
+ if replacement.try_reserve_exact(capacity).is_err() {
88
+ self.memory.release(capacity);
89
+ return Err(());
90
+ }
91
+ // Account for any excess capacity supplied by the allocator too.
92
+ let extra = replacement.capacity() - capacity;
93
+ if extra != 0 && !self.memory.reserve(extra) {
94
+ drop(replacement);
95
+ self.memory.release(capacity);
96
+ return Err(());
97
+ }
98
+ replacement.extend_from_slice(&self.data);
99
+ let old = std::mem::replace(&mut self.data, replacement);
100
+ let old_capacity = old.capacity();
101
+ drop(old);
102
+ self.memory.release(old_capacity);
103
+ }
104
+ self.data.extend_from_slice(bytes);
105
+ Ok(())
106
+ }
107
+
108
+ pub(crate) fn consume(&mut self, bytes: usize) {
109
+ if bytes == self.data.len() {
110
+ self.clear();
111
+ } else {
112
+ self.data.drain(..bytes);
113
+ }
114
+ }
115
+
116
+ pub(crate) fn copy_range(&self, range: std::ops::Range<usize>) -> Result<Self, ()> {
117
+ let mut copy = Self::new(self.memory.clone());
118
+ copy.extend_from_slice(&self.data[range])?;
119
+ Ok(copy)
120
+ }
121
+
122
+ pub(crate) fn clear(&mut self) {
123
+ let old = std::mem::take(&mut self.data);
124
+ let capacity = old.capacity();
125
+ drop(old);
126
+ self.memory.release(capacity);
127
+ }
128
+
129
+ #[cfg(test)]
130
+ pub(crate) fn capacity(&self) -> usize {
131
+ self.data.capacity()
132
+ }
133
+ }
134
+
135
+ impl Deref for ParserBuffer {
136
+ type Target = [u8];
137
+ fn deref(&self) -> &[u8] {
138
+ &self.data
139
+ }
140
+ }
141
+
142
+ impl Drop for ParserBuffer {
143
+ fn drop(&mut self) {
144
+ self.clear();
145
+ }
146
+ }
147
+
148
+ #[cfg(test)]
149
+ mod tests {
150
+ use super::*;
151
+
152
+ #[test]
153
+ fn children_enforce_local_and_shared_limits_and_roll_back_refusals() {
154
+ let shared = ParserMemory::new(128);
155
+ let local = ParserMemory::child(shared.clone(), 64);
156
+ let other = ParserMemory::child(shared.clone(), 128);
157
+ let mut a = ParserBuffer::new(local.clone());
158
+ let mut b = ParserBuffer::new(other.clone());
159
+ a.extend_from_slice(&[0; 64]).unwrap();
160
+ assert!(a.extend_from_slice(&[0; 1]).is_err());
161
+ b.extend_from_slice(&[0; 64]).unwrap();
162
+ assert!(b.extend_from_slice(&[0; 1]).is_err());
163
+ assert_eq!(shared.used(), 128);
164
+ assert_eq!(local.used(), 64);
165
+ assert_eq!(other.used(), 64);
166
+ drop(a);
167
+ drop(b);
168
+ assert_eq!(shared.used(), 0);
169
+ assert_eq!(local.used(), 0);
170
+ assert_eq!(other.used(), 0);
171
+ }
172
+
173
+ #[test]
174
+ fn charges_capacity_and_releases_on_consumption_and_drop() {
175
+ let memory = ParserMemory::new(128);
176
+ let mut a = ParserBuffer::new(memory.clone());
177
+ a.extend_from_slice(&[1; 33]).unwrap();
178
+ assert_eq!(memory.used(), a.capacity());
179
+ a.consume(32);
180
+ assert_eq!(memory.used(), a.capacity()); // remaining one byte retains capacity
181
+ let mut b = ParserBuffer::new(memory.clone());
182
+ b.extend_from_slice(&[2; 64]).unwrap();
183
+ assert!(a.extend_from_slice(&[3; 64]).is_err());
184
+ assert_eq!(a.len(), 1); // refusal does not mutate the existing buffer
185
+ a.consume(1);
186
+ assert_eq!(memory.used(), b.capacity());
187
+ drop(b);
188
+ assert_eq!(memory.used(), 0);
189
+ }
190
+
191
+ #[test]
192
+ fn growth_reserves_old_and_new_allocations_before_allocating() {
193
+ let memory = ParserMemory::new(100);
194
+ let mut buf = ParserBuffer::new(memory.clone());
195
+ buf.extend_from_slice(&[0; 32]).unwrap();
196
+ buf.extend_from_slice(&[0; 1]).unwrap();
197
+ assert_eq!(memory.used(), 64);
198
+ assert!(buf.extend_from_slice(&[0; 32]).is_err());
199
+ assert_eq!(memory.used(), 64);
200
+ buf.clear();
201
+ assert_eq!(memory.used(), 0);
202
+ }
203
+ }