@voxgig/sdkgen 4.50.0 → 4.51.0

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 (180) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/package.json +1 -1
  3. package/project/.sdk/model/feature/cache.aontu +6 -4
  4. package/project/.sdk/model/target/lua.aontu +6 -0
  5. package/project/.sdk/src/cmp/c/Config_c.ts +3 -134
  6. package/project/.sdk/src/cmp/c/TestClean_c.ts +14 -0
  7. package/project/.sdk/src/cmp/clojure/TestClean_clojure.ts +10 -5
  8. package/project/.sdk/src/cmp/cpp/TestClean_cpp.ts +13 -0
  9. package/project/.sdk/src/cmp/csharp/TestClean_csharp.ts +9 -0
  10. package/project/.sdk/src/cmp/elixir/TestClean_elixir.ts +11 -3
  11. package/project/.sdk/src/cmp/go/TestClean_go.ts +39 -3
  12. package/project/.sdk/src/cmp/java/TestClean_java.ts +8 -0
  13. package/project/.sdk/src/cmp/js/TestClean_js.ts +13 -5
  14. package/project/.sdk/src/cmp/kotlin/TestClean_kotlin.ts +22 -0
  15. package/project/.sdk/src/cmp/lua/TestClean_lua.ts +12 -3
  16. package/project/.sdk/src/cmp/ocaml/TestClean_ocaml.ts +18 -4
  17. package/project/.sdk/src/cmp/perl/TestClean_perl.ts +12 -3
  18. package/project/.sdk/src/cmp/php/TestClean_php.ts +10 -3
  19. package/project/.sdk/src/cmp/py/TestClean_py.ts +10 -3
  20. package/project/.sdk/src/cmp/rb/TestClean_rb.ts +10 -3
  21. package/project/.sdk/src/cmp/rust/TestClean_rust.ts +10 -0
  22. package/project/.sdk/src/cmp/scala/TestClean_scala.ts +34 -0
  23. package/project/.sdk/src/cmp/swift/TestClean_swift.ts +20 -10
  24. package/project/.sdk/src/cmp/ts/TestClean_ts.ts +13 -5
  25. package/project/.sdk/src/cmp/zig/TestClean_zig.ts +12 -0
  26. package/project/.sdk/tm/c/core/context.c +1 -0
  27. package/project/.sdk/tm/c/core/sdk.h +7 -4
  28. package/project/.sdk/tm/c/feature/cache.c +243 -13
  29. package/project/.sdk/tm/c/feature/secrets.c +27 -38
  30. package/project/.sdk/tm/c/tests/feature_test.c +104 -0
  31. package/project/.sdk/tm/c/utility/clean.c +26 -33
  32. package/project/.sdk/tm/c/utility/make_options.c +9 -8
  33. package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +14 -11
  34. package/project/.sdk/tm/clojure/src/sdk/client.clj +3 -1
  35. package/project/.sdk/tm/clojure/src/sdk/core.clj +19 -11
  36. package/project/.sdk/tm/clojure/src/sdk/features.clj +81 -10
  37. package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +33 -0
  38. package/project/.sdk/tm/clojure/test/sdk/test/pipeline.clj +1 -0
  39. package/project/.sdk/tm/cpp/Makefile +5 -0
  40. package/project/.sdk/tm/cpp/core/types.hpp +11 -4
  41. package/project/.sdk/tm/cpp/feature/audit.hpp +6 -7
  42. package/project/.sdk/tm/cpp/feature/cache.hpp +188 -9
  43. package/project/.sdk/tm/cpp/feature/secrets.hpp +12 -0
  44. package/project/.sdk/tm/cpp/test/feature_test.cpp +99 -0
  45. package/project/.sdk/tm/cpp/utility/pipeline.hpp +9 -7
  46. package/project/.sdk/tm/csharp/core/Context.cs +4 -0
  47. package/project/.sdk/tm/csharp/feature/CacheFeature.cs +126 -12
  48. package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +9 -27
  49. package/project/.sdk/tm/csharp/test/FeatureTest.cs +76 -0
  50. package/project/.sdk/tm/csharp/utility/Clean.cs +5 -10
  51. package/project/.sdk/tm/csharp/utility/Fetcher.cs +7 -1
  52. package/project/.sdk/tm/csharp/utility/MakeOptions.cs +5 -12
  53. package/project/.sdk/tm/elixir/lib/projectname/context.ex +2 -0
  54. package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +146 -30
  55. package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +10 -56
  56. package/project/.sdk/tm/elixir/lib/projectname/utility.ex +18 -12
  57. package/project/.sdk/tm/elixir/test/feature_test.exs +61 -0
  58. package/project/.sdk/tm/go/core/context.go +6 -0
  59. package/project/.sdk/tm/go/feature/cache_feature.go +131 -4
  60. package/project/.sdk/tm/go/feature/secrets_feature.go +17 -71
  61. package/project/.sdk/tm/go/test/feature_corpus_test.go +150 -2
  62. package/project/.sdk/tm/go/test/feature_test.go +61 -0
  63. package/project/.sdk/tm/go/utility/clean.go +5 -7
  64. package/project/.sdk/tm/go/utility/fetcher.go +11 -0
  65. package/project/.sdk/tm/go/utility/make_options.go +5 -7
  66. package/project/.sdk/tm/java/core/Context.java +9 -0
  67. package/project/.sdk/tm/java/feature/CacheFeature.java +123 -17
  68. package/project/.sdk/tm/java/feature/SecretsFeature.java +9 -92
  69. package/project/.sdk/tm/java/test/FeatureCorpusTest.java +141 -2
  70. package/project/.sdk/tm/java/test/FeatureTest.java +60 -0
  71. package/project/.sdk/tm/java/utility/Clean.java +6 -10
  72. package/project/.sdk/tm/java/utility/Fetcher.java +5 -1
  73. package/project/.sdk/tm/java/utility/MakeOptions.java +3 -1
  74. package/project/.sdk/tm/js/src/Context.js +4 -0
  75. package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +118 -11
  76. package/project/.sdk/tm/js/src/feature/cost/CostFeature.js +3 -2
  77. package/project/.sdk/tm/js/src/feature/metrics/MetricsFeature.js +1 -1
  78. package/project/.sdk/tm/js/src/feature/paging/PagingFeature.js +4 -4
  79. package/project/.sdk/tm/js/src/feature/rbac/RbacFeature.js +4 -4
  80. package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +16 -6
  81. package/project/.sdk/tm/js/src/feature/validate/ValidateFeature.js +2 -1
  82. package/project/.sdk/tm/js/src/utility/CleanUtility.js +1 -1
  83. package/project/.sdk/tm/js/src/utility/MakeOptionsUtility.js +6 -6
  84. package/project/.sdk/tm/js/test/feature/Corpus.test.js +100 -2
  85. package/project/.sdk/tm/js/test/feature/harness.js +3 -3
  86. package/project/.sdk/tm/js/test/pipeline.test.js +1 -1
  87. package/project/.sdk/tm/kotlin/core/Context.kt +3 -0
  88. package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +102 -7
  89. package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +10 -20
  90. package/project/.sdk/tm/kotlin/test/FeatureTest.kt +59 -0
  91. package/project/.sdk/tm/kotlin/utility/CoreUtil.kt +5 -5
  92. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +4 -0
  93. package/project/.sdk/tm/kotlin/utility/MakeOptions.kt +3 -1
  94. package/project/.sdk/tm/lua/core/context.lua +6 -0
  95. package/project/.sdk/tm/lua/feature/cache_feature.lua +212 -15
  96. package/project/.sdk/tm/lua/feature/secrets_feature.lua +15 -80
  97. package/project/.sdk/tm/lua/test/feature_test.lua +81 -0
  98. package/project/.sdk/tm/lua/utility/clean.lua +6 -9
  99. package/project/.sdk/tm/lua/utility/fetcher.lua +9 -0
  100. package/project/.sdk/tm/lua/utility/make_options.lua +5 -5
  101. package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +21 -6
  102. package/project/.sdk/tm/ocaml/sdk_features.ml +106 -8
  103. package/project/.sdk/tm/ocaml/sdk_runtime.ml +13 -10
  104. package/project/.sdk/tm/ocaml/sdk_types.ml +2 -0
  105. package/project/.sdk/tm/ocaml/test/t_feature.ml +35 -1
  106. package/project/.sdk/tm/perl/core/context.pm +3 -0
  107. package/project/.sdk/tm/perl/feature/audit_feature.pm +4 -7
  108. package/project/.sdk/tm/perl/feature/cache_feature.pm +111 -15
  109. package/project/.sdk/tm/perl/feature/secrets_feature.pm +13 -18
  110. package/project/.sdk/tm/perl/t/feature.t +66 -0
  111. package/project/.sdk/tm/perl/t/feature_corpus.t +80 -4
  112. package/project/.sdk/tm/perl/utility/clean.pm +6 -8
  113. package/project/.sdk/tm/perl/utility/fetcher.pm +4 -0
  114. package/project/.sdk/tm/perl/utility/make_options.pm +5 -5
  115. package/project/.sdk/tm/php/core/Context.php +4 -0
  116. package/project/.sdk/tm/php/feature/CacheFeature.php +98 -13
  117. package/project/.sdk/tm/php/feature/SecretsFeature.php +18 -15
  118. package/project/.sdk/tm/php/test/FeatureCorpusTest.php +113 -3
  119. package/project/.sdk/tm/php/test/FeatureTest.php +68 -0
  120. package/project/.sdk/tm/php/utility/Clean.php +3 -10
  121. package/project/.sdk/tm/php/utility/Fetcher.php +8 -3
  122. package/project/.sdk/tm/php/utility/MakeOptions.php +5 -7
  123. package/project/.sdk/tm/py/pkg/core/context.py +4 -0
  124. package/project/.sdk/tm/py/pkg/feature/cache_feature.py +79 -13
  125. package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +15 -66
  126. package/project/.sdk/tm/py/pkg/utility/clean.py +4 -10
  127. package/project/.sdk/tm/py/pkg/utility/make_options.py +8 -9
  128. package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +23 -9
  129. package/project/.sdk/tm/py/test/test_feature.py +50 -0
  130. package/project/.sdk/tm/py/test/test_feature_corpus.py +88 -3
  131. package/project/.sdk/tm/rb/core/context.rb +4 -1
  132. package/project/.sdk/tm/rb/feature/audit_feature.rb +6 -5
  133. package/project/.sdk/tm/rb/feature/cache_feature.rb +75 -11
  134. package/project/.sdk/tm/rb/feature/secrets_feature.rb +9 -64
  135. package/project/.sdk/tm/rb/test/feature_corpus_test.rb +85 -3
  136. package/project/.sdk/tm/rb/test/feature_test.rb +74 -0
  137. package/project/.sdk/tm/rb/utility/clean.rb +6 -7
  138. package/project/.sdk/tm/rb/utility/fetcher.rb +6 -0
  139. package/project/.sdk/tm/rb/utility/make_options.rb +5 -5
  140. package/project/.sdk/tm/rust/core/context.rs +8 -0
  141. package/project/.sdk/tm/rust/feature/cache.rs +120 -3
  142. package/project/.sdk/tm/rust/feature/secrets.rs +36 -98
  143. package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +3 -3
  144. package/project/.sdk/tm/rust/tests/feature_test.rs +100 -0
  145. package/project/.sdk/tm/rust/utility/clean.rs +35 -43
  146. package/project/.sdk/tm/rust/utility/fetcher.rs +6 -0
  147. package/project/.sdk/tm/rust/utility/make_options.rs +8 -11
  148. package/project/.sdk/tm/scala/core/Context.scala +4 -0
  149. package/project/.sdk/tm/scala/feature/CacheFeature.scala +117 -11
  150. package/project/.sdk/tm/scala/feature/SecretsFeature.scala +11 -24
  151. package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +28 -0
  152. package/project/.sdk/tm/scala/utility/Fetcher.scala +5 -0
  153. package/project/.sdk/tm/scala/utility/Make.scala +3 -1
  154. package/project/.sdk/tm/scala/utility/Misc.scala +5 -8
  155. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Context.swift +3 -0
  156. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +122 -11
  157. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +9 -13
  158. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Fetcher.swift +4 -0
  159. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/MakeOptions.swift +5 -4
  160. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/ResultUtil.swift +4 -5
  161. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +55 -0
  162. package/project/.sdk/tm/ts/src/Context.ts +4 -0
  163. package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +115 -4
  164. package/project/.sdk/tm/ts/src/feature/cost/CostFeature.ts +3 -2
  165. package/project/.sdk/tm/ts/src/feature/metrics/MetricsFeature.ts +1 -1
  166. package/project/.sdk/tm/ts/src/feature/paging/PagingFeature.ts +4 -4
  167. package/project/.sdk/tm/ts/src/feature/rbac/RbacFeature.ts +4 -4
  168. package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +14 -2
  169. package/project/.sdk/tm/ts/src/feature/validate/ValidateFeature.ts +2 -1
  170. package/project/.sdk/tm/ts/src/utility/CleanUtility.ts +1 -1
  171. package/project/.sdk/tm/ts/src/utility/MakeOptionsUtility.ts +6 -6
  172. package/project/.sdk/tm/ts/test/feature/Corpus.test.ts +101 -2
  173. package/project/.sdk/tm/ts/test/feature/harness.ts +3 -3
  174. package/project/.sdk/tm/ts/test/pipeline.test.ts +1 -1
  175. package/project/.sdk/tm/zig/core/context.zig +5 -0
  176. package/project/.sdk/tm/zig/core/utility.zig +11 -9
  177. package/project/.sdk/tm/zig/feature/cache.zig +144 -13
  178. package/project/.sdk/tm/zig/feature/secrets.zig +6 -1
  179. package/project/.sdk/tm/zig/test/feature_test.zig +65 -0
  180. package/project/sdkgen-package.json +1 -1
@@ -1,6 +1,8 @@
1
1
 
2
2
  use std::cell::RefCell;
3
+ use std::collections::hash_map::RandomState;
3
4
  use std::collections::HashMap;
5
+ use std::hash::{BuildHasher, Hash, Hasher};
4
6
  use std::rc::Rc;
5
7
 
6
8
  use crate::core::context::Context;
@@ -20,6 +22,7 @@ pub struct CacheSnapshot {
20
22
 
21
23
  pub struct CacheEntry {
22
24
  expiry: i64,
25
+ scope: String,
23
26
  snapshot: Rc<CacheSnapshot>,
24
27
  }
25
28
 
@@ -27,6 +30,10 @@ pub struct CacheEntry {
27
30
  pub struct CacheTrack {
28
31
  store: HashMap<String, CacheEntry>,
29
32
  order: Vec<String>,
33
+ // No HMAC in std: SipHash under this client's random keys separates
34
+ // header sets within one store without holding a header value. It is not
35
+ // a cryptographic commitment.
36
+ secret: RandomState,
30
37
 
31
38
  // Activity tracking (mirrors the ts client._cache record).
32
39
  pub hit: i64,
@@ -100,10 +107,14 @@ fn through(
100
107
  let methods = fopt_str_list(options, "methods").unwrap_or_else(|| vec!["GET".to_string()]);
101
108
  let cacheable = methods.iter().any(|m| m.to_uppercase() == method);
102
109
  if !cacheable {
103
- return inner(ctx, url, fetchdef);
110
+ let out = inner(ctx, url, fetchdef);
111
+ if method != "GET" && method != "HEAD" {
112
+ invalidate(&mut track.borrow_mut(), url);
113
+ }
114
+ return out;
104
115
  }
105
116
 
106
- let key = format!("{} {}", method, url);
117
+ let key = format!("{} {} {}", method, url, digest(&track.borrow().secret, &getp(fetchdef, "headers")));
107
118
  let now = fopt_now(options)();
108
119
 
109
120
  {
@@ -122,13 +133,19 @@ fn through(
122
133
  if let Ok(res) = &out {
123
134
  if storable(res) && json(res) {
124
135
  let snap = Rc::new(snapshot(res));
125
- let ttl = fopt_int(options, "ttl", 5000);
136
+ let ttl = lifetime(&snap.headers, fopt_int(options, "ttl", 5000));
126
137
  let mut t = track.borrow_mut();
138
+ if ttl <= 0 {
139
+ t.bypass += 1;
140
+ return Ok(replay(&snap));
141
+ }
142
+ drop_entry(&mut t, &key);
127
143
  evict(&mut t, options);
128
144
  t.store.insert(
129
145
  key.clone(),
130
146
  CacheEntry {
131
147
  expiry: now + ttl,
148
+ scope: url_scope(url),
132
149
  snapshot: snap.clone(),
133
150
  },
134
151
  );
@@ -142,6 +159,106 @@ fn through(
142
159
  out
143
160
  }
144
161
 
162
+ fn header_text(v: &Value) -> Option<String> {
163
+ match v {
164
+ Value::Noval | Value::Null => None,
165
+ Value::Str(s) => Some(s.clone()),
166
+ Value::Num(n) if n.fract() == 0.0 => Some(format!("{}", *n as i64)),
167
+ Value::Num(n) => Some(n.to_string()),
168
+ Value::Bool(b) => Some(b.to_string()),
169
+ other => Some(format!("{:?}", other)),
170
+ }
171
+ }
172
+
173
+ fn digest(secret: &RandomState, headers: &Value) -> String {
174
+ let mut pairs: Vec<(String, String)> = Vec::new();
175
+ if let Value::Map(hm) = headers {
176
+ for (k, v) in hm.borrow().iter() {
177
+ if let Some(text) = header_text(v) {
178
+ pairs.push((k.to_lowercase(), text));
179
+ }
180
+ }
181
+ }
182
+ pairs.sort();
183
+ let mut hasher = secret.build_hasher();
184
+ for (k, v) in &pairs {
185
+ format!("{}:{}\n", k, v).hash(&mut hasher);
186
+ }
187
+ format!("{:016x}", hasher.finish())
188
+ }
189
+
190
+ fn invalidate(t: &mut CacheTrack, url: &str) {
191
+ let path = url_scope(url);
192
+ let collection = url_parent(&path);
193
+ let dropped: Vec<String> = t
194
+ .store
195
+ .iter()
196
+ .filter(|(_, e)| scope_under(&e.scope, &path) || e.scope == collection)
197
+ .map(|(k, _)| k.clone())
198
+ .collect();
199
+ for key in dropped {
200
+ drop_entry(t, &key);
201
+ }
202
+ }
203
+
204
+ fn drop_entry(t: &mut CacheTrack, key: &str) {
205
+ if t.store.remove(key).is_some() {
206
+ t.order.retain(|k| k != key);
207
+ }
208
+ }
209
+
210
+ // The configured ttl capped by the response's Cache-Control; zero when the
211
+ // response may not be stored.
212
+ fn lifetime(headers: &Value, ttl: i64) -> i64 {
213
+ let mut control = String::new();
214
+ if let Value::Map(hm) = headers {
215
+ for (k, v) in hm.borrow().iter() {
216
+ if k.to_lowercase() == "cache-control" {
217
+ control = header_text(v).unwrap_or_default().to_lowercase();
218
+ }
219
+ }
220
+ }
221
+ let mut life = ttl;
222
+ for part in control.split(',') {
223
+ let directive = part.trim();
224
+ if ["no-store", "private", "no-cache"].contains(&directive) {
225
+ return 0;
226
+ }
227
+ if let Some(rest) = directive.strip_prefix("max-age") {
228
+ if let Some(value) = rest.trim_start().strip_prefix('=') {
229
+ let digits = value.trim().trim_matches('"');
230
+ if !digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit()) {
231
+ if let Ok(secs) = digits.parse::<i64>() {
232
+ life = life.min(secs.saturating_mul(1000));
233
+ }
234
+ }
235
+ }
236
+ }
237
+ }
238
+ life
239
+ }
240
+
241
+ fn url_scope(url: &str) -> String {
242
+ let base = url.split(|c| c == '?' || c == '#').next().unwrap_or("");
243
+ base.trim_end_matches('/').to_string()
244
+ }
245
+
246
+ fn url_parent(scope: &str) -> String {
247
+ let root = match scope.find("://") {
248
+ None => 0,
249
+ Some(scheme) => match scope[scheme + 3..].find('/') {
250
+ None => return scope.to_string(),
251
+ Some(slash) => scheme + 3 + slash,
252
+ },
253
+ };
254
+ let cut = scope.rfind('/').unwrap_or(0);
255
+ scope[..cut.max(root)].to_string()
256
+ }
257
+
258
+ fn scope_under(scope: &str, prefix: &str) -> bool {
259
+ scope == prefix || scope.starts_with(&format!("{}/", prefix))
260
+ }
261
+
145
262
  // A body the transport could not read as JSON passes through, for the operation to report.
146
263
  fn storable(res: &Value) -> bool {
147
264
  matches!(fres_status(res), Some(status) if (200..300).contains(&status))
@@ -5,12 +5,12 @@ pub mod sekreto;
5
5
 
6
6
  use std::cell::RefCell;
7
7
  use std::collections::BTreeMap;
8
- use std::rc::Rc;
8
+ use std::rc::{Rc, Weak};
9
9
 
10
10
  use crate::core::context::Context;
11
11
  use crate::core::error::ProjectNameError;
12
12
  use crate::core::helpers::{
13
- call_json, call_vfn, get_str, getp, getpath, ja, jo, json_thunk, setp, to_int, vfn,
13
+ call_json, call_vfn, get_str, getp, getpath, jo, setp, to_int, vfn,
14
14
  };
15
15
  use crate::core::types::{Feature, FetcherFn};
16
16
  use crate::feature::support::{fopt_bool, fopt_int, fopt_list, fopt_map, fopt_str, fres_status};
@@ -65,7 +65,10 @@ pub struct SecretsState {
65
65
  /// The client's options, READ-ONLY. This feature never writes them -
66
66
  /// see the header note.
67
67
  options: Value,
68
+ /// The client's clean registry, which a resolved value joins.
69
+ cleancfg: Value,
68
70
  mode: String,
71
+ rootctx: Weak<Context>,
69
72
 
70
73
  /// What prepare_auth was generated from: the config declares auth only
71
74
  /// where it places a credential, and Basic only where it composes one.
@@ -75,10 +78,8 @@ pub struct SecretsState {
75
78
  cred: String,
76
79
  refresh: String,
77
80
 
78
- /// A settled SUCCESSFUL resolution. Kept only while caching is on
79
- /// (`cache: false` means every request asks the chain again), and
80
- /// never set by a FAILURE - a transient vault outage must not poison
81
- /// the client permanently.
81
+ /// A settled SUCCESSFUL resolution, kept only while caching is on and
82
+ /// never set by a FAILURE, so a vault outage cannot poison the client.
82
83
  resolved: bool,
83
84
 
84
85
  /// A chain that could not be built. The transport gate refuses to send
@@ -95,7 +96,9 @@ impl SecretsState {
95
96
  cache: true,
96
97
  exchange: None,
97
98
  options: Value::Noval,
99
+ cleancfg: Value::Noval,
98
100
  mode: "live".to_string(),
101
+ rootctx: Weak::new(),
99
102
  declared: false,
100
103
  basic: false,
101
104
  cred: String::new(),
@@ -217,6 +220,7 @@ impl Feature for SecretsFeature {
217
220
  }
218
221
 
219
222
  let mut state = SecretsState::empty();
223
+ state.rootctx = Rc::downgrade(ctx);
220
224
 
221
225
  state.mode = ctx
222
226
  .client
@@ -225,6 +229,7 @@ impl Feature for SecretsFeature {
225
229
  .map(|c| c.mode.borrow().clone())
226
230
  .unwrap_or_else(|| "live".to_string());
227
231
  state.options = ctx.options.borrow().clone();
232
+ state.cleancfg = ctx.cleancfg.borrow().clone();
228
233
 
229
234
  let config = ctx.config.borrow().clone();
230
235
  let declared = getpath(&["options", "auth"], &config);
@@ -272,7 +277,7 @@ impl Feature for SecretsFeature {
272
277
  let mut specs: Vec<ProviderSpec> = Vec::new();
273
278
 
274
279
  if !explicit.is_empty() {
275
- register(&state.options, &explicit);
280
+ register(&state.cleancfg, &explicit);
276
281
  match envkey(&state.secretname, "") {
277
282
  Ok(key) => {
278
283
  let mut values: BTreeMap<String, String> = BTreeMap::new();
@@ -465,7 +470,7 @@ fn resolve_once(state: &Rc<RefCell<SecretsState>>) -> Result<bool, ProjectNameEr
465
470
  let found = found.map_err(|err| fail(&format!("{}", err)))?;
466
471
 
467
472
  if let Some(value) = &found {
468
- register(&state.borrow().options, value);
473
+ register(&state.borrow().cleancfg, value);
469
474
  }
470
475
 
471
476
  {
@@ -565,10 +570,8 @@ fn with_refresh(
565
570
  }
566
571
 
567
572
  // Another request may have bought a token while this one was in
568
- // flight. Spend what is current before buying: a second exchange
569
- // for a token that is already fresh is wasted, and on a provider
570
- // that invalidates the previous credential on issuance it breaks
571
- // the first request's own retry.
573
+ // flight: spend what is current rather than buy a second, which a
574
+ // provider invalidating on issuance would break this retry with.
572
575
  let current = state.borrow().cred.clone();
573
576
 
574
577
  let token = if !current.is_empty() && current != used {
@@ -576,10 +579,8 @@ fn with_refresh(
576
579
  } else {
577
580
  match buy(state) {
578
581
  Ok(token) => token,
579
- // The purchase failed: answer with the API's own refusal
580
- // rather than this one. The caller asked for data, and the
581
- // refusal is the more useful of the two - the exchange
582
- // error is a symptom.
582
+ // The purchase failed: answer with the API's own refusal,
583
+ // the more useful of the two.
583
584
  Err(_) => return out,
584
585
  }
585
586
  };
@@ -693,25 +694,26 @@ fn buy(state: &Rc<RefCell<SecretsState>>) -> Result<String, ProjectNameError> {
693
694
 
694
695
  let token = buy_once(state)?;
695
696
  state.borrow_mut().cred = token.clone();
696
- register(&state.borrow().options, &token);
697
+ register(&state.borrow().cleancfg, &token);
697
698
 
698
699
  Ok(token)
699
700
  }
700
701
 
701
702
  // A resolved or bought value arrives under no option key the intake
702
- // registration saw; the options map is the client's own, Rc-shared.
703
- fn register(options: &Value, value: &str) {
704
- crate::utility::clean::clean_add_opts(options, value);
703
+ // registration saw; the registry is the client's own, Rc-shared.
704
+ fn register(cleancfg: &Value, value: &str) {
705
+ crate::utility::clean::clean_add_cfg(cleancfg, value);
705
706
  }
706
707
 
707
708
  fn buy_once(state: &Rc<RefCell<SecretsState>>) -> Result<String, ProjectNameError> {
708
- let (refresh, exchange, options, secretname) = {
709
+ let (refresh, exchange, options, secretname, rootctx) = {
709
710
  let s = state.borrow();
710
711
  (
711
712
  s.refresh.clone(),
712
713
  s.exchange.clone(),
713
714
  s.options.clone(),
714
715
  s.secretname.clone(),
716
+ s.rootctx.upgrade(),
715
717
  )
716
718
  };
717
719
 
@@ -754,21 +756,13 @@ fn buy_once(state: &Rc<RefCell<SecretsState>>) -> Result<String, ProjectNameErro
754
756
  ),
755
757
  ("body", Value::str(body)),
756
758
  ("redirect", Value::str("manual")),
759
+ // Its own bound, in milliseconds: the timeout feature's wrapper is above it.
760
+ ("timeout", Value::Num(30000.0)),
757
761
  ]);
758
762
 
759
- let sys_fetch = getpath(&["system", "fetch"], &options);
760
-
761
- let fetch = |at: &str| -> Result<Value, ProjectNameError> {
762
- if let Value::Func(_) = sys_fetch {
763
- let out = call_vfn(&sys_fetch, &ja(vec![Value::str(at), fetchdef.clone()]));
764
- if let Some(msg) = get_str(&out, "__err__") {
765
- return Err(fail(&msg));
766
- }
767
- Ok(out)
768
- } else {
769
- raw_exchange_fetch(at, &fetchdef)
770
- }
771
- };
763
+ // The SDK's own transport beneath every wrapper: the test block and GET-only guard.
764
+ let rootctx = rootctx.ok_or_else(|| fail("secrets: the client is gone"))?;
765
+ let fetch = |at: &str| crate::utility::fetcher::fetcher_util(&rootctx, at, &fetchdef);
772
766
 
773
767
  let mut res = fetch(&url)?;
774
768
 
@@ -816,6 +810,14 @@ fn buy_once(state: &Rc<RefCell<SecretsState>>) -> Result<String, ProjectNameErro
816
810
 
817
811
  const REDIRECT_MAX: usize = 5;
818
812
 
813
+ // An agent that follows nothing, for reading URLs the way ureq sends them.
814
+ fn exchange_agent() -> ureq::Agent {
815
+ static AGENT: std::sync::OnceLock<ureq::Agent> = std::sync::OnceLock::new();
816
+ AGENT
817
+ .get_or_init(|| ureq::AgentBuilder::new().redirects(0).build())
818
+ .clone()
819
+ }
820
+
819
821
  // The URL a body-keeping redirect names, while it stays on the exchange's own
820
822
  // origin. Any other redirect would drop the body or leave the origin.
821
823
  fn moved(url: &str, at: &str, res: &Value) -> Option<String> {
@@ -834,67 +836,3 @@ fn moved(url: &str, at: &str, res: &Value) -> Option<String> {
834
836
  None
835
837
  }
836
838
  }
837
-
838
- // One pooled agent for every exchange; a followed redirect would carry the token.
839
- fn exchange_agent() -> ureq::Agent {
840
- static AGENT: std::sync::OnceLock<ureq::Agent> = std::sync::OnceLock::new();
841
- AGENT
842
- .get_or_init(|| ureq::AgentBuilder::new().redirects(0).build())
843
- .clone()
844
- }
845
-
846
- fn raw_exchange_fetch(fullurl: &str, fetchdef: &Value) -> Result<Value, ProjectNameError> {
847
- let method = get_str(fetchdef, "method")
848
- .filter(|m| !m.is_empty())
849
- .unwrap_or_else(|| "POST".to_string());
850
-
851
- let agent = exchange_agent();
852
- let mut req = agent.request(&method, fullurl);
853
-
854
- if let Value::Map(m) = getp(fetchdef, "headers") {
855
- for (k, v) in m.borrow().iter() {
856
- if let Value::Str(sv) = v {
857
- req = req.set(k, sv);
858
- }
859
- }
860
- }
861
-
862
- let body = get_str(fetchdef, "body").unwrap_or_default();
863
-
864
- let sent = if body.is_empty() {
865
- req.call()
866
- } else {
867
- req.send_string(&body)
868
- };
869
-
870
- let resp = match sent {
871
- Ok(resp) => resp,
872
- Err(ureq::Error::Status(_code, resp)) => resp,
873
- Err(err) => return Err(fail(&format!("secrets: token exchange: {}", err))),
874
- };
875
-
876
- let status = resp.status() as i64;
877
-
878
- let headers = Value::empty_map();
879
- for name in resp.headers_names() {
880
- if let Some(val) = resp.header(&name) {
881
- setp(&headers, &name.to_lowercase(), Value::str(val));
882
- }
883
- }
884
-
885
- let text = resp
886
- .into_string()
887
- .map_err(|err| fail(&format!("secrets: token exchange body: {}", err)))?;
888
-
889
- let parsed = if text.is_empty() {
890
- Value::Noval
891
- } else {
892
- crate::utility::jsonparse::json_parse(&text).unwrap_or(Value::Noval)
893
- };
894
-
895
- Ok(jo(vec![
896
- ("status", Value::Num(status as f64)),
897
- ("headers", headers),
898
- ("json", json_thunk(parsed)),
899
- ]))
900
- }
@@ -1660,7 +1660,7 @@ fn exchange_auth_null_suppresses_and_is_never_retried() {
1660
1660
  }
1661
1661
 
1662
1662
  #[test]
1663
- fn the_exchange_has_a_raw_transport_when_no_system_fetch_is_given() {
1663
+ fn the_exchange_goes_through_the_sdk_transport_when_no_system_fetch_is_given() {
1664
1664
  if skipped_without_auth() {
1665
1665
  return;
1666
1666
  }
@@ -1690,8 +1690,8 @@ fn the_exchange_has_a_raw_transport_when_no_system_fetch_is_given() {
1690
1690
  assert_eq!(getp(&res, "ok"), Value::Bool(false));
1691
1691
  let err = get_str(&res, "err").unwrap_or_default();
1692
1692
  assert!(
1693
- err.contains("token exchange"),
1694
- "expected the exchange's own refusal from the raw transport, got: {}",
1693
+ err.contains("/api/auth/token"),
1694
+ "expected the SDK transport's failure for the token endpoint, got: {}",
1695
1695
  err
1696
1696
  );
1697
1697
  }
@@ -772,6 +772,106 @@ fn feature_cache_inactive_does_not_wrap() {
772
772
  assert_eq!(calls.borrow().len(), 2, "expected 2 calls");
773
773
  }
774
774
 
775
+ fn fh_cache_load(h: &FhHarness, path: &str, headers: Value) {
776
+ h.op(FhOpSpec {
777
+ op: "load".to_string(),
778
+ path: path.to_string(),
779
+ headers,
780
+ ..Default::default()
781
+ });
782
+ }
783
+
784
+ #[test]
785
+ fn feature_cache_keys_on_request_headers() {
786
+ if !fh_present(&["cache"]) {
787
+ return;
788
+ }
789
+ let (server, calls) = fh_recorder(None);
790
+ let f = Rc::new(RefCell::new(CacheFeature::new()));
791
+ let h = fh_make(Some(server), vec![(fr(&f), jo(vec![("ttl", Value::Num(10000.0))]))]);
792
+ fh_cache_load(&h, "/w/1", jo(vec![("x-tenant", Value::str("tenant-a"))]));
793
+ fh_cache_load(&h, "/w/1", jo(vec![("X-Tenant", Value::str("tenant-b"))]));
794
+ assert_eq!(calls.borrow().len(), 2, "a second tenant was served the first tenant's entry");
795
+ fh_cache_load(&h, "/w/1", jo(vec![("x-tenant", Value::str("tenant-b"))]));
796
+ assert_eq!(calls.borrow().len(), 2, "the same headers did not share an entry");
797
+ }
798
+
799
+ #[test]
800
+ fn feature_cache_a_write_drops_its_collection() {
801
+ if !fh_present(&["cache"]) {
802
+ return;
803
+ }
804
+ let (server, calls) = fh_recorder(None);
805
+ let f = Rc::new(RefCell::new(CacheFeature::new()));
806
+ let h = fh_make(Some(server), vec![(fr(&f), jo(vec![("ttl", Value::Num(10000.0))]))]);
807
+ let paths = ["/w/1", "/w", "/w/1/part", "/other/1", "/w/2"];
808
+ for p in paths {
809
+ fh_cache_load(&h, p, Value::Noval);
810
+ }
811
+ h.op(FhOpSpec {
812
+ op: "update".to_string(),
813
+ method: "PUT".to_string(),
814
+ path: "/w/1".to_string(),
815
+ ..Default::default()
816
+ });
817
+ for p in paths {
818
+ fh_cache_load(&h, p, Value::Noval);
819
+ }
820
+ assert_eq!(calls.borrow().len(), 9, "expected the collection refetched and /other cached");
821
+ assert_eq!(f.borrow().track.borrow().hit, 2, "expected /other served from cache");
822
+ }
823
+
824
+ #[test]
825
+ fn feature_cache_control_prevents_storing() {
826
+ if !fh_present(&["cache"]) {
827
+ return;
828
+ }
829
+ for control in ["no-store", "private", "No-Cache", "max-age=60, private"] {
830
+ let (server, calls) = fh_recorder(Some(Rc::new(move |_n, _fd| {
831
+ Ok(fh_response(
832
+ 200,
833
+ jo(vec![("ok", Value::Bool(true))]),
834
+ jo(vec![("Cache-Control", Value::str(control))]),
835
+ ))
836
+ })));
837
+ let f = Rc::new(RefCell::new(CacheFeature::new()));
838
+ let h = fh_make(Some(server), vec![(fr(&f), jo(vec![("ttl", Value::Num(10000.0))]))]);
839
+ fh_cache_load(&h, "/w", Value::Noval);
840
+ fh_cache_load(&h, "/w", Value::Noval);
841
+ assert_eq!(calls.borrow().len(), 2, "{} was stored", control);
842
+ }
843
+ }
844
+
845
+ #[test]
846
+ fn feature_cache_max_age_caps_the_ttl() {
847
+ if !fh_present(&["cache"]) {
848
+ return;
849
+ }
850
+ let clock = FhClock::new();
851
+ let (server, calls) = fh_recorder(Some(Rc::new(|_n, _fd| {
852
+ Ok(fh_response(
853
+ 200,
854
+ jo(vec![("ok", Value::Bool(true))]),
855
+ jo(vec![("cache-control", Value::str("max-age=1"))]),
856
+ ))
857
+ })));
858
+ let f = Rc::new(RefCell::new(CacheFeature::new()));
859
+ let h = fh_make(
860
+ Some(server),
861
+ vec![(
862
+ fr(&f),
863
+ jo(vec![("ttl", Value::Num(10000.0)), ("now", clock.now_fn())]),
864
+ )],
865
+ );
866
+ fh_cache_load(&h, "/w", Value::Noval);
867
+ clock.advance(500);
868
+ fh_cache_load(&h, "/w", Value::Noval);
869
+ assert_eq!(calls.borrow().len(), 1, "expected a hit inside max-age");
870
+ clock.advance(600);
871
+ fh_cache_load(&h, "/w", Value::Noval);
872
+ assert_eq!(calls.borrow().len(), 2, "expected max-age=1 to expire the entry before the ttl");
873
+ }
874
+
775
875
  // --- idempotency ----------------------------------------------------------------
776
876
 
777
877
  #[test]