@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
@@ -3,7 +3,7 @@ use std::rc::Rc;
3
3
 
4
4
  use crate::core::context::Context;
5
5
  use crate::core::error::ProjectNameError;
6
- use crate::core::helpers::{getp, getpath, jo, setp};
6
+ use crate::core::helpers::{getp, jo, setp};
7
7
  use crate::core::response::{header_value, is_binary_response, Response};
8
8
  use crate::core::spec::Spec;
9
9
  use crate::utility::media::{is_raw_request, RAW_BODY};
@@ -13,8 +13,8 @@ use crate::utility::voxgigstruct::Value;
13
13
  // Every registered secret VALUE (and its encoded forms) is replaced wherever
14
14
  // it appears in a string, and every value under a sensitive KEY name is
15
15
  // masked whatever it holds. The registry is the Rc-shared `values` list of
16
- // `options.__derived__.clean`, so a feature registering a value later
17
- // reaches the list every context reads.
16
+ // the context's `cleancfg`, held beside the options rather than in them, so a
17
+ // feature registering a value later reaches the list every context reads.
18
18
 
19
19
  const MAXDEPTH: usize = 32;
20
20
  const CIRCULAR: &str = "[circular]";
@@ -95,8 +95,8 @@ fn count(val: &Value, dflt: usize) -> usize {
95
95
  }
96
96
  }
97
97
 
98
- /// The derived clean block from a clean option block: what make_options
99
- /// stores under `__derived__.clean`.
98
+ /// The clean registry from a clean option block: what make_options stores
99
+ /// on the context as `cleancfg`.
100
100
  pub fn make_clean_config(cleanopts: &Value) -> Value {
101
101
  let active = !matches!(getp(cleanopts, "active"), Value::Bool(false));
102
102
  let mask = match getp(cleanopts, "mask") {
@@ -116,18 +116,13 @@ pub fn make_clean_config(cleanopts: &Value) -> Value {
116
116
  ])
117
117
  }
118
118
 
119
- fn derived_block(options: &Value) -> Value {
120
- getpath(&["__derived__", "clean"], options)
121
- }
122
-
123
- // A context without options (make_error is reached with a bare one) falls
119
+ // A context without a registry (make_error is reached with a bare one) falls
124
120
  // back to the schema defaults, so nothing leaves raw for want of a
125
121
  // constructor.
126
122
  fn config_block(ctx: &Context) -> Value {
127
- let options = ctx.options.borrow().clone();
128
- let derived = derived_block(&options);
129
- if let Value::Map(_) = derived {
130
- return derived;
123
+ let cfg = ctx.cleancfg.borrow().clone();
124
+ if let Value::Map(_) = cfg {
125
+ return cfg;
131
126
  }
132
127
  make_clean_config(&getp(&crate::core::schema::optspec(), "clean"))
133
128
  }
@@ -192,23 +187,22 @@ fn forms(value: &str) -> Vec<String> {
192
187
  out
193
188
  }
194
189
 
195
- /// Register a secret value against an options map. Idempotent; shorter
196
- /// than `min` is not a secret the SDK can mask without blanking ordinary
197
- /// text. A map with no derived block registers nothing.
198
- pub fn clean_add_opts(options: &Value, value: &str) {
199
- let block = derived_block(options);
190
+ /// Register a secret value in a clean registry. Idempotent; shorter than
191
+ /// `min` is not a secret the SDK can mask without blanking ordinary text.
192
+ /// Anything but a registry map registers nothing.
193
+ pub fn clean_add_cfg(block: &Value, value: &str) {
200
194
  if !matches!(block, Value::Map(_)) {
201
195
  return;
202
196
  }
203
- let min = count(&getp(&block, "min"), 4).max(1);
197
+ let min = count(&getp(block, "min"), 4).max(1);
204
198
  if value.chars().count() < min {
205
199
  return;
206
200
  }
207
- let list = match getp(&block, "values") {
201
+ let list = match getp(block, "values") {
208
202
  Value::List(l) => l,
209
203
  _ => {
210
204
  let l = Value::empty_list();
211
- setp(&block, "values", l.clone());
205
+ setp(block, "values", l.clone());
212
206
  match l {
213
207
  Value::List(l) => l,
214
208
  _ => return,
@@ -236,10 +230,10 @@ pub fn clean_add_opts(options: &Value, value: &str) {
236
230
  }
237
231
  }
238
232
 
239
- /// Register a secret value the context's options will mask from now on.
233
+ /// Register a secret value the context's registry will mask from now on.
240
234
  pub fn clean_add(ctx: &Rc<Context>, value: &str) {
241
- let options = ctx.options.borrow().clone();
242
- clean_add_opts(&options, value);
235
+ let cfg = ctx.cleancfg.borrow().clone();
236
+ clean_add_cfg(&cfg, value);
243
237
  }
244
238
 
245
239
  fn mask_value(cfg: &CleanConfig, value: &str) -> String {
@@ -478,14 +472,13 @@ pub fn clean_str(ctx: &Rc<Context>, val: &str) -> String {
478
472
  clean_string(&cfg, val)
479
473
  }
480
474
 
481
- /// Clean a value against an options map rather than a context, for a
482
- /// feature that holds the options but no context.
483
- pub fn clean_opts(options: &Value, val: &Value) -> Value {
484
- let block = derived_block(options);
475
+ /// Clean a value against a registry rather than a context, for a feature
476
+ /// that holds the registry but no context.
477
+ pub fn clean_cfg(block: &Value, val: &Value) -> Value {
485
478
  if !matches!(block, Value::Map(_)) {
486
479
  return val.clone();
487
480
  }
488
- clean_with(&read_config(&block), None, val)
481
+ clean_with(&read_config(block), None, val)
489
482
  }
490
483
 
491
484
  /// Is this key name sensitive under the context's clean configuration?
@@ -508,25 +501,24 @@ pub fn clean_error(ctx: &Rc<Context>, mut err: ProjectNameError) -> ProjectNameE
508
501
  }
509
502
 
510
503
  /// Every scalar under a sensitive name, at any depth and of any shape, is
511
- /// registered against `options`: a credential mistyped as a map or a number
512
- /// is still a credential.
513
- pub fn clean_add_sensitive_opts(options: &Value, val: &Value) {
514
- let block = derived_block(options);
504
+ /// registered in `block`: a credential mistyped as a map or a number is
505
+ /// still a credential.
506
+ pub fn clean_add_sensitive_cfg(block: &Value, val: &Value) {
515
507
  if !matches!(block, Value::Map(_)) {
516
508
  return;
517
509
  }
518
- let cfg = read_config(&block);
519
- add_sensitive(&cfg, options, val, false, 0, &mut Vec::new());
510
+ let cfg = read_config(block);
511
+ add_sensitive(&cfg, block, val, false, 0, &mut Vec::new());
520
512
  }
521
513
 
522
514
  pub fn clean_add_sensitive(ctx: &Rc<Context>, val: &Value) {
523
- let options = ctx.options.borrow().clone();
524
- clean_add_sensitive_opts(&options, val);
515
+ let cfg = ctx.cleancfg.borrow().clone();
516
+ clean_add_sensitive_cfg(&cfg, val);
525
517
  }
526
518
 
527
519
  fn add_sensitive(
528
520
  cfg: &CleanConfig,
529
- options: &Value,
521
+ block: &Value,
530
522
  val: &Value,
531
523
  under: bool,
532
524
  depth: usize,
@@ -536,8 +528,8 @@ fn add_sensitive(
536
528
  return;
537
529
  }
538
530
  match val {
539
- Value::Str(s) if under => clean_add_opts(options, s),
540
- Value::Num(n) if under => clean_add_opts(options, &vs::value::num_to_string(*n)),
531
+ Value::Str(s) if under => clean_add_cfg(block, s),
532
+ Value::Num(n) if under => clean_add_cfg(block, &vs::value::num_to_string(*n)),
541
533
  Value::List(_) | Value::Map(_) => {
542
534
  let ptr = node_ptr(val).unwrap_or(0);
543
535
  if seen.contains(&ptr) {
@@ -547,13 +539,13 @@ fn add_sensitive(
547
539
  match val {
548
540
  Value::List(l) => {
549
541
  for v in l.borrow().iter() {
550
- add_sensitive(cfg, options, v, under, depth + 1, seen);
542
+ add_sensitive(cfg, block, v, under, depth + 1, seen);
551
543
  }
552
544
  }
553
545
  Value::Map(m) => {
554
546
  for (k, v) in m.borrow().iter() {
555
547
  let sub = under || sensitive_key(cfg, Some(k));
556
- add_sensitive(cfg, options, v, sub, depth + 1, seen);
548
+ add_sensitive(cfg, block, v, sub, depth + 1, seen);
557
549
  }
558
550
  }
559
551
  _ => {}
@@ -133,6 +133,12 @@ fn default_http_fetch(
133
133
 
134
134
  let resp = loop {
135
135
  let mut req = agent.request(&method, &url);
136
+ // A bound in milliseconds, for a caller beneath the timeout feature.
137
+ if let Value::Num(ms) = getp(fetchdef, "timeout") {
138
+ if 0.0 < ms {
139
+ req = req.timeout(std::time::Duration::from_millis(ms as u64));
140
+ }
141
+ }
136
142
  for (name, value) in &headers {
137
143
  req = req.set(name, value);
138
144
  }
@@ -61,8 +61,8 @@ pub fn make_options_util(ctx: &Rc<Context>) -> Result<Value, ProjectNameError> {
61
61
  let optspec = crate::core::schema::optspec();
62
62
 
63
63
  // The secret registry exists BEFORE validation, fed from the raw input.
64
- // Its block is Rc-shared: what is registered into `cleanopts` here is
65
- // what `opts.__derived__.clean` carries out below.
64
+ // Its block is Rc-shared: what is registered here is what the context's
65
+ // `cleancfg` carries out below.
66
66
  let cleancfg = clean::make_clean_config(&vs::merge(
67
67
  &ja(vec![
68
68
  Value::empty_map(),
@@ -72,11 +72,10 @@ pub fn make_options_util(ctx: &Rc<Context>) -> Result<Value, ProjectNameError> {
72
72
  ]),
73
73
  None,
74
74
  ));
75
- let cleanopts = jo(vec![("__derived__", jo(vec![("clean", cleancfg.clone())]))]);
76
- clean::clean_add_sensitive_opts(&cleanopts, &without(&opts, &["clean"]));
75
+ clean::clean_add_sensitive_cfg(&cleancfg, &without(&opts, &["clean"]));
77
76
  for src in [&cfgopts, &opts] {
78
77
  for value in clean::splitvalues(&getpath(&["clean", "values"], src)) {
79
- clean::clean_add_opts(&cleanopts, &value);
78
+ clean::clean_add_cfg(&cleancfg, &value);
80
79
  }
81
80
  }
82
81
 
@@ -200,14 +199,12 @@ pub fn make_options_util(ctx: &Rc<Context>) -> Result<Value, ProjectNameError> {
200
199
  let order_list =
201
200
  Value::list(feature_order.into_iter().map(|n| Value::str(n)).collect());
202
201
 
203
- setp(
204
- &opts,
205
- "__derived__",
206
- jo(vec![("clean", cleancfg), ("featureorder", order_list)]),
207
- );
202
+ setp(&opts, "__derived__", jo(vec![("featureorder", order_list)]));
203
+
204
+ *ctx.cleancfg.borrow_mut() = cleancfg.clone();
208
205
 
209
206
  // Again over the merged result: the config's own defaults can carry one.
210
- clean::clean_add_sensitive_opts(&opts, &without(&opts, &["clean", "__derived__"]));
207
+ clean::clean_add_sensitive_cfg(&cleancfg, &without(&opts, &["clean", "__derived__"]));
211
208
 
212
209
  Ok(opts)
213
210
  }
@@ -18,6 +18,8 @@ class Context(ctxmap: JMap[String, Object], basectx: Context) {
18
18
  var config: JMap[String, Object] = null
19
19
  var entopts: JMap[String, Object] = null
20
20
  var options: JMap[String, Object] = null
21
+ // The clean registry, beside the options rather than in them.
22
+ var cleancfg: Object = null
21
23
  var opmap: JMap[String, Operation] = null
22
24
  var response: Response = null
23
25
  var result: Result = null
@@ -73,6 +75,8 @@ class Context(ctxmap: JMap[String, Object], basectx: Context) {
73
75
  // Options
74
76
  Helpers.getCtxProp(ctxmap, "options") match { case o: JMap[_, _] => options = o.asInstanceOf[JMap[String, Object]]; case _ => }
75
77
  if (options == null && basectx != null) options = basectx.options
78
+ cleancfg = Helpers.getCtxProp(ctxmap, "cleancfg")
79
+ if (cleancfg == null && basectx != null) cleancfg = basectx.cleancfg
76
80
 
77
81
  // Entity
78
82
  Helpers.getCtxProp(ctxmap, "entity") match { case e: Entity => entity = e; case _ => }
@@ -1,24 +1,23 @@
1
1
  package SCALAPACKAGE.feature
2
2
 
3
+ import java.nio.charset.StandardCharsets
4
+ import java.security.SecureRandom
3
5
  import java.util.{ArrayList, LinkedHashMap, List => JList, Map => JMap}
4
6
  import java.util.function.Supplier
7
+ import javax.crypto.Mac
8
+ import javax.crypto.spec.SecretKeySpec
5
9
  import SCALAPACKAGE.core.{Context, FetcherFn, SdkClient}
6
10
  import SCALAPACKAGE.utility.Media
7
11
 
8
- // Response caching for safe (read) requests. Wraps the active transport and
9
- // serves a fresh cached snapshot instead of hitting the network when the
10
- // same method+URL was fetched within `ttl` ms (default: 5000). Only
11
- // successful (2xx) responses to cacheable methods (default: GET) are
12
- // stored, keyed by method+URL. The cache is bounded (`max` entries, default
13
- // 256, oldest evicted first) and every hit/miss/bypass is counted. Bodies
14
- // are snapshotted on capture so both the current caller and later hits can
15
- // re-read the JSON body repeatedly.
12
+ // Response caching for safe (read) requests, per client: see the features
13
+ // reference for the key, the write invalidation and the Cache-Control rules.
16
14
  class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
17
15
 
18
16
  private var client: SdkClient = null
19
17
  private var options: JMap[String, Object] = null
20
18
  private var store: JMap[String, CacheEntry] = null
21
19
  private var order: JList[String] = null
20
+ private var secret: SecretKeySpec = null
22
21
 
23
22
  // Activity tracking (mirrors the ts client._cache record).
24
23
  var hit: Int = 0
@@ -27,6 +26,7 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
27
26
 
28
27
  private class CacheEntry {
29
28
  var expiry: Long = 0
29
+ var scope: String = ""
30
30
  var snapshot: CacheSnapshot = null
31
31
  }
32
32
 
@@ -48,6 +48,9 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
48
48
 
49
49
  this.store = new LinkedHashMap[String, CacheEntry]()
50
50
  this.order = new ArrayList[String]()
51
+ val bytes = new Array[Byte](32)
52
+ new SecureRandom().nextBytes(bytes)
53
+ this.secret = new SecretKeySpec(bytes, "HmacSHA256")
51
54
 
52
55
  val inner: FetcherFn = ctx.utility.fetcher
53
56
 
@@ -77,10 +80,17 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
77
80
  }
78
81
  }
79
82
  if (!cacheable) {
80
- return inner(ctx, url, fetchdef)
83
+ if (CacheFeature.SafeMethods.contains(method)) {
84
+ return inner(ctx, url, fetchdef)
85
+ }
86
+ try {
87
+ return inner(ctx, url, fetchdef)
88
+ } finally {
89
+ invalidate(url)
90
+ }
81
91
  }
82
92
 
83
- val key = method + " " + url
93
+ val key = method + " " + url + " " + digest(fetchdef.get("headers"))
84
94
  val now = FeatureOptions.foptNow(this.options).getAsLong()
85
95
 
86
96
  val hitEntry = this.store.get(key)
@@ -106,10 +116,16 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
106
116
 
107
117
  if (storable(res)) {
108
118
  val snapshot = this.snapshot(res)
109
- val ttl = FeatureOptions.foptInt(this.options, "ttl", 5000)
119
+ val ttl = CacheFeature.lifetime(snapshot.headers, FeatureOptions.foptInt(this.options, "ttl", 5000).toLong)
120
+ if (ttl <= 0) {
121
+ this.bypass += 1
122
+ return replay(snapshot)
123
+ }
124
+ drop(key)
110
125
  evict()
111
126
  val entry = new CacheEntry()
112
127
  entry.expiry = now + ttl
128
+ entry.scope = CacheFeature.scope(url)
113
129
  entry.snapshot = snapshot
114
130
  this.store.put(key, entry)
115
131
  this.order.add(key)
@@ -121,6 +137,48 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
121
137
  res
122
138
  }
123
139
 
140
+ private def digest(headers: Object): String = {
141
+ val pairs = new ArrayList[(String, String)]()
142
+ headers match {
143
+ case hm: JMap[_, _] =>
144
+ val it = hm.entrySet().iterator()
145
+ while (it.hasNext) {
146
+ val h = it.next()
147
+ if (h.getValue != null) {
148
+ pairs.add((String.valueOf(h.getKey).toLowerCase(), String.valueOf(h.getValue)))
149
+ }
150
+ }
151
+ case _ =>
152
+ }
153
+ val sorted = pairs.toArray(new Array[(String, String)](0)).sortBy(identity)
154
+ val mac = Mac.getInstance("HmacSHA256")
155
+ mac.init(this.secret)
156
+ for ((name, value) <- sorted) {
157
+ mac.update((name + ":" + value + "\n").getBytes(StandardCharsets.UTF_8))
158
+ }
159
+ mac.doFinal().map(b => f"$b%02x").mkString
160
+ }
161
+
162
+ private def invalidate(url: String): Unit = {
163
+ val path = CacheFeature.scope(url)
164
+ val collection = CacheFeature.parent(path)
165
+ val dropped = new ArrayList[String]()
166
+ val it = this.store.entrySet().iterator()
167
+ while (it.hasNext) {
168
+ val e = it.next()
169
+ if (CacheFeature.under(e.getValue.scope, path) || e.getValue.scope == collection) {
170
+ dropped.add(e.getKey)
171
+ }
172
+ }
173
+ dropped.forEach(k => drop(k))
174
+ }
175
+
176
+ private def drop(key: String): Unit = {
177
+ if (this.store.remove(key) != null) {
178
+ this.order.remove(key)
179
+ }
180
+ }
181
+
124
182
  // A body the transport could not read as JSON passes through, for the operation to report.
125
183
  private def storable(res: Object): Boolean = {
126
184
  val status = FeatureOptions.fresStatus(res)
@@ -193,3 +251,51 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
193
251
  }
194
252
  }
195
253
  }
254
+
255
+
256
+ object CacheFeature {
257
+ private val SafeMethods = Set("GET", "HEAD")
258
+ private val Unstored = Set("no-store", "private", "no-cache")
259
+ private val MaxAge = "^max-age\\s*=\\s*\"?(\\d+)\"?$".r
260
+
261
+ // The configured ttl capped by the response's Cache-Control; zero when the
262
+ // response may not be stored.
263
+ private def lifetime(headers: JMap[String, Object], ttl: Long): Long = {
264
+ var control = ""
265
+ val it = headers.entrySet().iterator()
266
+ while (it.hasNext) {
267
+ val h = it.next()
268
+ if (h.getKey.toLowerCase() == "cache-control") {
269
+ control = String.valueOf(h.getValue).toLowerCase()
270
+ }
271
+ }
272
+ val directives = control.split(",").map(_.trim)
273
+ if (directives.exists(Unstored.contains)) {
274
+ 0L
275
+ } else {
276
+ directives.foldLeft(ttl) { (life, directive) =>
277
+ directive match {
278
+ case MaxAge(secs) => secs.toLongOption.fold(life)(n => math.min(life, n * 1000))
279
+ case _ => life
280
+ }
281
+ }
282
+ }
283
+ }
284
+
285
+ private def scope(url: String): String = {
286
+ var out = url.split("[?#]", 2)(0)
287
+ while (out.endsWith("/")) {
288
+ out = out.substring(0, out.length - 1)
289
+ }
290
+ out
291
+ }
292
+
293
+ private def parent(scope: String): String = {
294
+ val scheme = scope.indexOf("://")
295
+ val root = if (scheme < 0) 0 else scope.indexOf('/', scheme + 3)
296
+ if (root < 0) scope else scope.substring(0, math.max(scope.lastIndexOf('/'), root))
297
+ }
298
+
299
+ private def under(scope: String, prefix: String): Boolean =
300
+ scope == prefix || scope.startsWith(prefix + "/")
301
+ }
@@ -65,6 +65,7 @@ import voxgig.plugin.{Definition, VBool, VList, VMap, VNull, VNum, VStr, Value}
65
65
  class SecretsFeature extends BaseFeature("secrets", "0.1.0", true) {
66
66
 
67
67
  private var client: SdkClient = null
68
+ private var rootctx: Context = null
68
69
 
69
70
  // Every value this feature resolves or buys is a secret the SDK handles,
70
71
  // and none arrives under an option key the intake registration saw.
@@ -141,6 +142,7 @@ class SecretsFeature extends BaseFeature("secrets", "0.1.0", true) {
141
142
  // Sync by feature contract: build the chain, never look anything up here.
142
143
  override def init(ctx: Context, options: JMap[String, Object]): Unit = {
143
144
  this.client = ctx.client
145
+ this.rootctx = ctx
144
146
  this.liveopts = ctx.options
145
147
  this.active = FeatureOptions.foptBool(options, "active", false)
146
148
 
@@ -517,6 +519,8 @@ class SecretsFeature extends BaseFeature("secrets", "0.1.0", true) {
517
519
  fetchdef.put("body", Struct.jsonify(bodymap, flags))
518
520
  // Never followed by the client: a redirect would carry the refresh token on.
519
521
  fetchdef.put("redirect", "manual")
522
+ // Its own bound, in milliseconds: the timeout feature's wrapper is above it.
523
+ fetchdef.put("timeout", java.lang.Integer.valueOf(30000))
520
524
 
521
525
  var res = exchangefetch(url, fetchdef)
522
526
 
@@ -568,34 +572,17 @@ class SecretsFeature extends BaseFeature("secrets", "0.1.0", true) {
568
572
  token
569
573
  }
570
574
 
571
- // Deliberately NOT the SDK transport. The transport is what this feature
572
- // wraps, and sending the token request back through it would recurse on
573
- // the first expiry - and would route the exchange through the test mock,
574
- // which knows nothing about it. `system.fetch` when the caller supplied
575
- // one, else the platform fetcher directly: Fetcher.defaultHttpFetch
576
- // already answers in the {status, headers, json} shape the seam promises
577
- // and, unlike Fetcher.fetcher, is gated on neither mode nor the test
578
- // feature.
575
+ // The SDK's own transport, beneath every feature's wrapper: it holds the
576
+ // test feature's block and the GET-only guard, and the wrapped one would
577
+ // recurse on the first expiry.
579
578
  private def exchangefetch(
580
579
  url: String,
581
580
  fetchdef: JMap[String, Object],
582
- ): JMap[String, Object] = {
583
- var sysfetch: Object = null
584
- if (this.liveopts != null) {
585
- sysfetch = Struct.getpath(this.liveopts, java.util.List.of("system", "fetch"))
586
- if (sysfetch eq Struct.UNDEF) sysfetch = null
587
- }
588
-
589
- sysfetch match {
590
- case bf: BiFunction[_, _, _] =>
591
- bf.asInstanceOf[BiFunction[String, JMap[String, Object], Object]]
592
- .apply(url, fetchdef) match {
593
- case m: JMap[_, _] => m.asInstanceOf[JMap[String, Object]]
594
- case _ => null
595
- }
596
- case _ => Fetcher.defaultHttpFetch(url, fetchdef)
581
+ ): JMap[String, Object] =
582
+ Fetcher.fetcher(this.rootctx, url, fetchdef) match {
583
+ case m: JMap[_, _] => m.asInstanceOf[JMap[String, Object]]
584
+ case _ => null
597
585
  }
598
- }
599
586
 
600
587
  // Where a 307 or 308 points, when that is on the exchange's own origin. Any
601
588
  // other redirect would drop the body or leave the origin.
@@ -428,6 +428,34 @@ object SdkTestMain {
428
428
  (sent.get(1).get("headers") eq b.ctx.response.headers),
429
429
  "the cache handed on a copy it had read")
430
430
  eqI("cache.notJson.bypass", 2, f.bypass) }
431
+ { val rec = new FhRecorder()
432
+ val h = fhMake((c, u, fd) => rec.fetch(c, u, fd), fhF(new CacheFeature(), om("ttl" -> I(10000))))
433
+ h.op(fhOp("load").withPath("/w/1").withHeaders(om("x-tenant" -> "tenant-a")))
434
+ h.op(fhOp("load").withPath("/w/1").withHeaders(om("X-Tenant" -> "tenant-b")))
435
+ eqI("cache.tenantHeader", 2, rec.calls.size())
436
+ h.op(fhOp("load").withPath("/w/1").withHeaders(om("x-tenant" -> "tenant-b")))
437
+ eqI("cache.sameHeaders", 2, rec.calls.size()) }
438
+ { val rec = new FhRecorder(); val f = new CacheFeature()
439
+ val h = fhMake((c, u, fd) => rec.fetch(c, u, fd), fhF(f, om("ttl" -> I(10000))))
440
+ val paths = Seq("/w/1", "/w", "/w/1/part", "/other/1", "/w/2")
441
+ paths.foreach(p => h.op(fhOp("load").withPath(p)))
442
+ h.op(fhOp("update").withMethod("PUT").withPath("/w/1"))
443
+ paths.foreach(p => h.op(fhOp("load").withPath(p)))
444
+ eqI("cache.writeDrops", 9, rec.calls.size()); eqI("cache.writeKeepsOther", 2, f.hit) }
445
+ for (control <- Seq("no-store", "private", "No-Cache", "max-age=60, private")) {
446
+ val rec = new FhRecorder()
447
+ rec.reply = (n, fd) => fhResponse(200, om("ok" -> B(true)), om("Cache-Control" -> control))
448
+ val h = fhMake((c, u, fd) => rec.fetch(c, u, fd), fhF(new CacheFeature(), om("ttl" -> I(10000))))
449
+ h.op(fhOp("load").withPath("/w")); h.op(fhOp("load").withPath("/w"))
450
+ eqI("cache.unstored " + control, 2, rec.calls.size())
451
+ }
452
+ { val clk = new FhClock(); val rec = new FhRecorder()
453
+ rec.reply = (n, fd) => fhResponse(200, om("ok" -> B(true)), om("cache-control" -> "max-age=1"))
454
+ val h = fhMake((c, u, fd) => rec.fetch(c, u, fd), fhF(new CacheFeature(), om("ttl" -> I(10000), "now" -> f0(clk.nowFn))))
455
+ h.op(fhOp("load").withPath("/w")); clk.advance(500); h.op(fhOp("load").withPath("/w"))
456
+ eqI("cache.insideMaxAge", 1, rec.calls.size())
457
+ clk.advance(600); h.op(fhOp("load").withPath("/w"))
458
+ eqI("cache.maxAge", 2, rec.calls.size()) }
431
459
  }
432
460
 
433
461
  private def testIdempotency(): Unit = {
@@ -174,6 +174,11 @@ object Fetcher {
174
174
  val reqb =
175
175
  try HttpRequest.newBuilder(URI.create(fullurl)).method(method, bodyPublisher)
176
176
  catch { case e: RuntimeException => throw new RuntimeException("fetch: invalid url: " + fullurl, e) }
177
+ // A bound in milliseconds, for a caller beneath the timeout feature.
178
+ fetchdef.get("timeout") match {
179
+ case n: java.lang.Number if 0 < n.longValue() => reqb.timeout(java.time.Duration.ofMillis(n.longValue()))
180
+ case _ =>
181
+ }
177
182
 
178
183
  var hasUA = false
179
184
  fetchdef.get("headers") match {
@@ -475,10 +475,12 @@ object MakeOptions {
475
475
  }
476
476
 
477
477
  val derived = new LinkedHashMap[String, Object]()
478
- derived.put("clean", cleancfg)
479
478
  derived.put("featureorder", featureorder)
480
479
  opts.put("__derived__", derived)
481
480
 
481
+ // Beside the options, not in them: optionsMap hands them to callers.
482
+ ctx.cleancfg = cleancfg
483
+
482
484
  // Again over the merged result: the config's own defaults can carry one.
483
485
  addSensitiveOptions(cleancfg, opts, "clean", "__derived__")
484
486
 
@@ -83,15 +83,12 @@ object Clean {
83
83
  )
84
84
  }
85
85
 
86
- // A context without options (makeError accepts a bare one) still masks by
87
- // the schema defaults.
86
+ // A context without a registry (makeError accepts a bare one) still masks
87
+ // by the schema defaults.
88
88
  private def cleanConfig(ctx: Context): CleanConfig = {
89
- if (ctx != null && ctx.options != null) {
90
- val derived = Helpers.toMapAny(ctx.options.get("__derived__"))
91
- if (derived != null) derived.get("clean") match {
92
- case c: CleanConfig => return c
93
- case _ =>
94
- }
89
+ if (ctx != null) ctx.cleancfg match {
90
+ case c: CleanConfig => return c
91
+ case _ =>
95
92
  }
96
93
  makeCleanConfig(Schema.optspec.get("clean"))
97
94
  }
@@ -24,6 +24,8 @@ public final class Context {
24
24
  public var config: VMap?
25
25
  public var entopts: VMap?
26
26
  public var options: VMap?
27
+ // The clean registry, beside the options rather than in them.
28
+ public var cleancfg: Utility.CleanConfig?
27
29
  public var opmap: [String: Operation] = [:]
28
30
  public var response: Response?
29
31
  public var result: Result?
@@ -79,6 +81,7 @@ public final class Context {
79
81
  // Options
80
82
  if let om = ctxProp(ctxmap, "options") as? VMap { options = om }
81
83
  if options == nil, let b = basectx { options = b.options }
84
+ cleancfg = (ctxProp(ctxmap, "cleancfg") as? Utility.CleanConfig) ?? basectx?.cleancfg
82
85
 
83
86
  // Entity
84
87
  if let ent = ctxProp(ctxmap, "entity") as? Entity { entity = ent }