@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,31 +1,38 @@
1
1
  package JAVAPACKAGE.feature;
2
2
 
3
+ import java.nio.charset.StandardCharsets;
4
+ import java.security.GeneralSecurityException;
5
+ import java.security.SecureRandom;
3
6
  import java.util.ArrayList;
7
+ import java.util.Iterator;
4
8
  import java.util.LinkedHashMap;
5
9
  import java.util.List;
6
10
  import java.util.Map;
7
11
  import java.util.function.Supplier;
12
+ import java.util.regex.Matcher;
13
+ import java.util.regex.Pattern;
14
+
15
+ import javax.crypto.Mac;
16
+ import javax.crypto.spec.SecretKeySpec;
8
17
 
9
18
  import JAVAPACKAGE.core.Context;
10
19
  import JAVAPACKAGE.core.SdkClient;
11
20
  import JAVAPACKAGE.core.Utility;
12
21
  import JAVAPACKAGE.utility.Media;
13
22
 
14
- // Response caching for safe (read) requests. Wraps the active transport and
15
- // serves a fresh cached snapshot instead of hitting the network when the
16
- // same method+URL was fetched within `ttl` ms (default: 5000). Only
17
- // successful (2xx) responses to cacheable methods (default: GET) are
18
- // stored, keyed by method+URL. The cache is bounded (`max` entries, default
19
- // 256, oldest evicted first) and every hit/miss/bypass is counted. Bodies
20
- // are snapshotted on capture so both the current caller and later hits can
21
- // re-read the JSON body repeatedly.
23
+ // Response caching for safe (read) requests, per client: see the features
24
+ // reference for the key, the write invalidation and the Cache-Control rules.
22
25
  @SuppressWarnings({"unchecked"})
23
26
  public class CacheFeature extends BaseFeature {
24
27
 
25
28
  private SdkClient client;
26
29
  private Map<String, Object> options;
27
30
  private Map<String, CacheEntry> store;
28
- private List<String> order;
31
+ private SecretKeySpec secret;
32
+
33
+ private static final List<String> SAFE_METHODS = List.of("GET", "HEAD");
34
+ private static final List<String> UNSTORED = List.of("no-store", "private", "no-cache");
35
+ private static final Pattern MAX_AGE = Pattern.compile("^max-age\\s*=\\s*\"?(\\d+)\"?$");
29
36
 
30
37
  // Activity tracking (mirrors the ts client._cache record).
31
38
  public int hit = 0;
@@ -34,6 +41,7 @@ public class CacheFeature extends BaseFeature {
34
41
 
35
42
  private static final class CacheEntry {
36
43
  long expiry;
44
+ String scope;
37
45
  CacheSnapshot snapshot;
38
46
  }
39
47
 
@@ -59,7 +67,9 @@ public class CacheFeature extends BaseFeature {
59
67
  }
60
68
 
61
69
  this.store = new LinkedHashMap<>();
62
- this.order = new ArrayList<>();
70
+ byte[] key = new byte[32];
71
+ new SecureRandom().nextBytes(key);
72
+ this.secret = new SecretKeySpec(key, "HmacSHA256");
63
73
 
64
74
  final Utility.FetcherFn inner = ctx.utility.fetcher;
65
75
 
@@ -88,10 +98,18 @@ public class CacheFeature extends BaseFeature {
88
98
  }
89
99
  }
90
100
  if (!cacheable) {
91
- return inner.fetch(ctx, url, fetchdef);
101
+ if (SAFE_METHODS.contains(method)) {
102
+ return inner.fetch(ctx, url, fetchdef);
103
+ }
104
+ try {
105
+ return inner.fetch(ctx, url, fetchdef);
106
+ }
107
+ finally {
108
+ invalidate(url);
109
+ }
92
110
  }
93
111
 
94
- String key = method + " " + url;
112
+ String key = method + " " + url + " " + digest(fetchdef.get("headers"));
95
113
  long now = FeatureOptions.foptNow(this.options).getAsLong();
96
114
 
97
115
  CacheEntry hitEntry = this.store.get(key);
@@ -117,13 +135,18 @@ public class CacheFeature extends BaseFeature {
117
135
 
118
136
  if (storable(res)) {
119
137
  CacheSnapshot snapshot = snapshot(res);
120
- int ttl = FeatureOptions.foptInt(this.options, "ttl", 5000);
138
+ long ttl = lifetime(snapshot.headers, FeatureOptions.foptInt(this.options, "ttl", 5000));
139
+ if (ttl <= 0) {
140
+ this.bypass++;
141
+ return replay(snapshot);
142
+ }
143
+ this.store.remove(key);
121
144
  evict();
122
145
  CacheEntry entry = new CacheEntry();
123
146
  entry.expiry = now + ttl;
147
+ entry.scope = scope(url);
124
148
  entry.snapshot = snapshot;
125
149
  this.store.put(key, entry);
126
- this.order.add(key);
127
150
  this.miss++;
128
151
  return replay(snapshot);
129
152
  }
@@ -132,6 +155,88 @@ public class CacheFeature extends BaseFeature {
132
155
  return res;
133
156
  }
134
157
 
158
+ private String digest(Object headers) {
159
+ List<String[]> pairs = new ArrayList<>();
160
+ if (headers instanceof Map) {
161
+ for (Map.Entry<?, ?> h : ((Map<?, ?>) headers).entrySet()) {
162
+ if (h.getValue() != null) {
163
+ pairs.add(new String[] {String.valueOf(h.getKey()).toLowerCase(), String.valueOf(h.getValue())});
164
+ }
165
+ }
166
+ }
167
+ pairs.sort((a, b) -> a[0].equals(b[0]) ? a[1].compareTo(b[1]) : a[0].compareTo(b[0]));
168
+ try {
169
+ Mac mac = Mac.getInstance("HmacSHA256");
170
+ mac.init(this.secret);
171
+ for (String[] p : pairs) {
172
+ mac.update((p[0] + ":" + p[1] + "\n").getBytes(StandardCharsets.UTF_8));
173
+ }
174
+ StringBuilder hex = new StringBuilder();
175
+ for (byte b : mac.doFinal()) {
176
+ hex.append(String.format("%02x", b));
177
+ }
178
+ return hex.toString();
179
+ }
180
+ catch (GeneralSecurityException err) {
181
+ throw new IllegalStateException(err);
182
+ }
183
+ }
184
+
185
+ private void invalidate(String url) {
186
+ String path = scope(url);
187
+ String collection = parent(path);
188
+ this.store.values().removeIf((e) -> under(e.scope, path) || e.scope.equals(collection));
189
+ }
190
+
191
+ // The configured ttl capped by the response's Cache-Control; zero when the
192
+ // response may not be stored.
193
+ private static long lifetime(Map<String, Object> headers, long ttl) {
194
+ String control = "";
195
+ for (Map.Entry<String, Object> h : headers.entrySet()) {
196
+ if ("cache-control".equals(h.getKey().toLowerCase())) {
197
+ control = String.valueOf(h.getValue()).toLowerCase();
198
+ }
199
+ }
200
+ long life = ttl;
201
+ for (String part : control.split(",")) {
202
+ String directive = part.trim();
203
+ if (UNSTORED.contains(directive)) {
204
+ return 0;
205
+ }
206
+ Matcher age = MAX_AGE.matcher(directive);
207
+ if (age.matches()) {
208
+ try {
209
+ life = Math.min(life, Long.parseLong(age.group(1)) * 1000);
210
+ }
211
+ catch (NumberFormatException err) {
212
+ continue;
213
+ }
214
+ }
215
+ }
216
+ return life;
217
+ }
218
+
219
+ private static String scope(String url) {
220
+ String out = url.split("[?#]", 2)[0];
221
+ while (out.endsWith("/")) {
222
+ out = out.substring(0, out.length() - 1);
223
+ }
224
+ return out;
225
+ }
226
+
227
+ private static String parent(String scope) {
228
+ int scheme = scope.indexOf("://");
229
+ int root = scheme < 0 ? 0 : scope.indexOf('/', scheme + 3);
230
+ if (root < 0) {
231
+ return scope;
232
+ }
233
+ return scope.substring(0, Math.max(scope.lastIndexOf('/'), root));
234
+ }
235
+
236
+ private static boolean under(String scope, String prefix) {
237
+ return scope.equals(prefix) || scope.startsWith(prefix + "/");
238
+ }
239
+
135
240
  // A body the transport could not read as JSON passes through, for the operation to report.
136
241
  private boolean storable(Object res) {
137
242
  int status = FeatureOptions.fresStatus(res);
@@ -190,9 +295,10 @@ public class CacheFeature extends BaseFeature {
190
295
  // evict drops oldest entries (FIFO) until the store is under `max`.
191
296
  private void evict() {
192
297
  int max = FeatureOptions.foptInt(this.options, "max", 256);
193
- while (this.store.size() >= max && !this.order.isEmpty()) {
194
- String oldest = this.order.remove(0);
195
- this.store.remove(oldest);
298
+ Iterator<String> oldest = this.store.keySet().iterator();
299
+ while (this.store.size() >= max && oldest.hasNext()) {
300
+ oldest.next();
301
+ oldest.remove();
196
302
  }
197
303
  }
198
304
  }
@@ -1,12 +1,7 @@
1
1
  package JAVAPACKAGE.feature;
2
2
 
3
- import java.io.IOException;
4
3
  import java.net.URI;
5
- import java.net.http.HttpClient;
6
- import java.net.http.HttpRequest;
7
- import java.net.http.HttpResponse;
8
4
  import java.nio.charset.StandardCharsets;
9
- import java.time.Duration;
10
5
  import java.util.ArrayList;
11
6
  import java.util.Base64;
12
7
  import java.util.Locale;
@@ -14,7 +9,6 @@ import java.util.LinkedHashMap;
14
9
  import java.util.List;
15
10
  import java.util.Map;
16
11
  import java.util.concurrent.CountDownLatch;
17
- import java.util.function.BiFunction;
18
12
  import java.util.function.Supplier;
19
13
 
20
14
  import JAVAPACKAGE.core.Config;
@@ -695,9 +689,6 @@ public class SecretsFeature extends BaseFeature {
695
689
  }
696
690
  String url = base + "/" + path;
697
691
 
698
- Object sysRaw = opts.get("system");
699
- Object fetchRaw = sysRaw instanceof Map
700
- ? ((Map<String, Object>) sysRaw).get("fetch") : null;
701
692
 
702
693
  // The body is SERIALISED, never concatenated: a refresh token (or a
703
694
  // configured request-field name) carrying a quote, backslash or
@@ -714,12 +705,11 @@ public class SecretsFeature extends BaseFeature {
714
705
  fetchdef.put("body", Json.stringify(payload));
715
706
  // Never followed by the client: a redirect would carry the refresh token on.
716
707
  fetchdef.put("redirect", "manual");
708
+ // Its own bound, in milliseconds: the timeout feature's wrapper is above it.
709
+ fetchdef.put("timeout", 30000);
717
710
 
718
- // Deliberately NOT the SDK transport. The transport is what this
719
- // feature wraps, and sending the token request back through it would
720
- // recurse on the first expiry - and would route the exchange through
721
- // the test mock, which knows nothing about it.
722
- Object res = exchangeFetch(fetchRaw, url, fetchdef);
711
+ // Not the wrapped transport, which would recurse on the first expiry.
712
+ Object res = exchangeFetch(url, fetchdef);
723
713
 
724
714
  // By option alone, and only while the exchange stays on its own origin.
725
715
  String at = url;
@@ -729,7 +719,7 @@ public class SecretsFeature extends BaseFeature {
729
719
  break;
730
720
  }
731
721
  at = next;
732
- res = exchangeFetch(fetchRaw, at, fetchdef);
722
+ res = exchangeFetch(at, fetchdef);
733
723
  }
734
724
 
735
725
  int status = FeatureOptions.fresStatus(res);
@@ -764,17 +754,10 @@ public class SecretsFeature extends BaseFeature {
764
754
  return token;
765
755
  }
766
756
 
767
- private static Object exchangeFetch(Object fetchRaw, String url,
768
- Map<String, Object> fetchdef) {
769
- if (fetchRaw instanceof BiFunction) {
770
- return ((BiFunction<String, Map<String, Object>, Object>) fetchRaw)
771
- .apply(url, fetchdef);
772
- }
773
- // No custom transport supplied - the ordinary case. makeOptions
774
- // leaves system.fetch unset, so the exchange gets its own raw HTTP
775
- // path: requiring a custom transport for the COMMON case would
776
- // reject every live token purchase before a request was made.
777
- return rawExchangeFetch(url, fetchdef);
757
+ // The SDK's own transport, beneath every feature's wrapper: it holds the
758
+ // test feature's block and the GET-only guard.
759
+ private Object exchangeFetch(String url, Map<String, Object> fetchdef) {
760
+ return Fetcher.fetcher(this.rootctx, url, fetchdef);
778
761
  }
779
762
 
780
763
  // Where a 307 or 308 points, when that is on the exchange's own origin. Any
@@ -809,72 +792,6 @@ public class SecretsFeature extends BaseFeature {
809
792
  return scheme + "://" + host.toLowerCase(Locale.ROOT) + ":" + port;
810
793
  }
811
794
 
812
- // rawExchangeFetch is the token-exchange transport of last resort: plain
813
- // java.net.http, in the same result shape the system.fetch seam promises
814
- // ("status" + "json"). utility/Fetcher.defaultHttpFetch is
815
- // package-private in JAVAPACKAGE.utility and unreachable from here, and
816
- // reaching for it would put the exchange back on the SDK's own transport
817
- // stack - which is the thing this must not use.
818
- private static Map<String, Object> rawExchangeFetch(String fullurl,
819
- Map<String, Object> fetchdef) {
820
-
821
- Object methodRaw = fetchdef.get("method");
822
- String method = methodRaw instanceof String && !"".equals(methodRaw)
823
- ? (String) methodRaw : "POST";
824
-
825
- Object bodyRaw = fetchdef.get("body");
826
- String body = bodyRaw instanceof String ? (String) bodyRaw : "";
827
-
828
- HttpRequest.Builder reqb = HttpRequest.newBuilder(URI.create(fullurl))
829
- .method(method, "".equals(body)
830
- ? HttpRequest.BodyPublishers.noBody()
831
- : HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8));
832
-
833
- Object headersRaw = fetchdef.get("headers");
834
- if (headersRaw instanceof Map) {
835
- for (Map.Entry<String, Object> h : ((Map<String, Object>) headersRaw).entrySet()) {
836
- if (h.getValue() instanceof String) {
837
- reqb.setHeader(h.getKey(), (String) h.getValue());
838
- }
839
- }
840
- }
841
-
842
- HttpResponse<String> resp;
843
- try {
844
- resp = HttpClient.newBuilder()
845
- .connectTimeout(Duration.ofSeconds(30))
846
- .followRedirects(HttpClient.Redirect.NEVER)
847
- .build()
848
- .send(reqb.build(), HttpResponse.BodyHandlers.ofString());
849
- }
850
- catch (IOException | InterruptedException e) {
851
- if (e instanceof InterruptedException) {
852
- Thread.currentThread().interrupt();
853
- }
854
- throw new Sekreto.SekretoError(
855
- "secrets: token exchange transport failed: " + e.getMessage());
856
- }
857
-
858
- String text = resp.body() == null ? "" : resp.body();
859
- Object parsed;
860
- try {
861
- parsed = "".equals(text) ? null : Json.parse(text);
862
- }
863
- catch (RuntimeException e) {
864
- parsed = null;
865
- }
866
- final Object jsonBody = parsed;
867
-
868
- Map<String, Object> out = new LinkedHashMap<>();
869
- out.put("status", resp.statusCode());
870
- Map<String, Object> rheaders = new LinkedHashMap<>();
871
- resp.headers().firstValue("location").ifPresent((v) -> rheaders.put("location", v));
872
- out.put("headers", rheaders);
873
- out.put("json", (Supplier<Object>) () -> jsonBody);
874
- out.put("body", text);
875
- return out;
876
- }
877
-
878
795
  private static void awaitLatch(CountDownLatch latch) {
879
796
  try {
880
797
  latch.await();
@@ -203,9 +203,14 @@ public class FeatureCorpusTest {
203
203
 
204
204
  private static Object invoke(ProjectNameSDK client, Op op, Map<String, Object> ctrl)
205
205
  throws Exception {
206
+ return invoke(client, op, ctrl, new LinkedHashMap<String, Object>());
207
+ }
208
+
209
+ private static Object invoke(ProjectNameSDK client, Op op, Map<String, Object> ctrl,
210
+ Map<String, Object> args) throws Exception {
206
211
  Object ent = op.accessor.invoke(client, new Object[] {null});
207
212
  try {
208
- return op.call.invoke(ent, new LinkedHashMap<String, Object>(), ctrl);
213
+ return op.call.invoke(ent, new LinkedHashMap<String, Object>(args), ctrl);
209
214
  }
210
215
  catch (java.lang.reflect.InvocationTargetException ite) {
211
216
  // Unwrap: a reflective call reports the SDK's error as its cause, and a
@@ -241,6 +246,92 @@ public class FeatureCorpusTest {
241
246
  return picked;
242
247
  }
243
248
 
249
+ private static final List<String> READ_OPS = List.of("list", "load");
250
+ private static final List<String> WRITE_OPS = List.of("create", "update", "patch", "remove");
251
+
252
+ /** A read and a write of one entity (#READ, #WRITE), each driven as usableOps drives an op. */
253
+ private static Map<String, Op> usablePair() {
254
+ Map<String, Map<String, Op>> found = new LinkedHashMap<>();
255
+ for (Op cand : candidates(buildClient(Map.of()))) {
256
+ int dot = cand.key.indexOf('.');
257
+ String entity = cand.key.substring(0, dot);
258
+ String opname = cand.key.substring(dot + 1);
259
+ String role = READ_OPS.contains(opname) ? "read" : WRITE_OPS.contains(opname) ? "write" : null;
260
+ Map<String, Op> seen = found.computeIfAbsent(entity, (k) -> new LinkedHashMap<>());
261
+ if (null == role || seen.containsKey(role)) {
262
+ continue;
263
+ }
264
+ try {
265
+ invoke(buildClient(Map.of()), cand, new LinkedHashMap<>());
266
+ }
267
+ catch (Exception e) {
268
+ continue;
269
+ }
270
+ seen.put(role, cand);
271
+ if (seen.containsKey("read") && seen.containsKey("write")) {
272
+ return seen;
273
+ }
274
+ }
275
+ return null;
276
+ }
277
+
278
+ private static final Map<String, Object> ITEM_ARGS = Map.of("id", "corpus-item");
279
+
280
+ private static String entityOf(Op op) {
281
+ return op.key.substring(0, op.key.indexOf('.'));
282
+ }
283
+
284
+ /** A write of one entity (#ITEM) that completes when driven with ITEM_ARGS. */
285
+ private static Op usableItem(String entity) {
286
+ for (Op cand : candidates(buildClient(Map.of()))) {
287
+ String opname = cand.key.substring(cand.key.indexOf('.') + 1);
288
+ if (!entity.equals(entityOf(cand)) || !List.of("update", "patch", "remove").contains(opname)) {
289
+ continue;
290
+ }
291
+ try {
292
+ invoke(buildClient(Map.of()), cand, new LinkedHashMap<>(), ITEM_ARGS);
293
+ }
294
+ catch (Exception e) {
295
+ continue;
296
+ }
297
+ return cand;
298
+ }
299
+ return null;
300
+ }
301
+
302
+ /** One step: it may wait, may add request headers, and may pass `args` as the first argument. */
303
+ private static Object step(ProjectNameSDK client, Op op, Map<String, Object> step,
304
+ Map<String, Object> ctrl) throws Exception {
305
+ if (step.get("wait") instanceof Number) {
306
+ Thread.sleep(((Number) step.get("wait")).longValue());
307
+ }
308
+ Map<String, Object> args = step.get("args") instanceof Map
309
+ ? (Map<String, Object>) step.get("args") : new LinkedHashMap<>();
310
+ if (!(step.get("headers") instanceof Map)) {
311
+ return invoke(client, op, ctrl, args);
312
+ }
313
+ Map<String, Object> options = client.getRootCtx().options;
314
+ boolean had = options.containsKey("headers");
315
+ Object prior = options.get("headers");
316
+ Map<String, Object> merged = new LinkedHashMap<>();
317
+ if (prior instanceof Map) {
318
+ merged.putAll((Map<String, Object>) prior);
319
+ }
320
+ merged.putAll((Map<String, Object>) step.get("headers"));
321
+ options.put("headers", merged);
322
+ try {
323
+ return invoke(client, op, ctrl, args);
324
+ }
325
+ finally {
326
+ if (had) {
327
+ options.put("headers", prior);
328
+ }
329
+ else {
330
+ options.remove("headers");
331
+ }
332
+ }
333
+ }
334
+
244
335
  /** Replace #OPn throughout a case, keys included. */
245
336
  private static Object resolve(Object node, Map<String, String> tokens) {
246
337
  if (node instanceof String) {
@@ -448,6 +539,23 @@ public class FeatureCorpusTest {
448
539
  for (Op o : ops) {
449
540
  byKey.put(o.key, o);
450
541
  }
542
+ Map<String, Op> pair = usablePair();
543
+ if (null != pair) {
544
+ for (Op o : pair.values()) {
545
+ byKey.put(o.key, o);
546
+ }
547
+ }
548
+ Op item = null == pair ? null : usableItem(entityOf(pair.get("read")));
549
+ if (null != item) {
550
+ byKey.put(item.key, item);
551
+ }
552
+ Op other = null;
553
+ for (Op o : null == pair ? List.<Op>of() : ops) {
554
+ if (!entityOf(o).equals(entityOf(pair.get("read")))) {
555
+ other = o;
556
+ break;
557
+ }
558
+ }
451
559
 
452
560
  int ran = 0;
453
561
  for (Object rawCase : cases) {
@@ -460,6 +568,27 @@ public class FeatureCorpusTest {
460
568
  continue;
461
569
  }
462
570
 
571
+ boolean paired = java.util.regex.Pattern.compile("#(READ|WRITE|ITEM|OTHER)\\b")
572
+ .matcher(String.valueOf(rawCase)).find();
573
+ if (paired && null == pair) {
574
+ System.err.println(String.format(
575
+ "skip \"%s\": needs a read and a write of one entity, this SDK offers none", rawName));
576
+ continue;
577
+ }
578
+
579
+ String lacking = null;
580
+ if (paired && null == item && String.valueOf(rawCase).matches("(?s).*#ITEM\\b.*")) {
581
+ lacking = "#ITEM";
582
+ }
583
+ else if (paired && null == other && String.valueOf(rawCase).matches("(?s).*#OTHER\\b.*")) {
584
+ lacking = "#OTHER";
585
+ }
586
+ if (null != lacking) {
587
+ System.err.println(String.format(
588
+ "skip \"%s\": needs %s, which this SDK cannot drive", rawName, lacking));
589
+ continue;
590
+ }
591
+
463
592
  String missing = missingFeature(rawCase, have);
464
593
  if (null != missing) {
465
594
  System.err.println(String.format(
@@ -472,6 +601,16 @@ public class FeatureCorpusTest {
472
601
  for (int i = 0; i < need; i++) {
473
602
  tokens.put("#OP" + (i + 1), ops.get(i).key);
474
603
  }
604
+ if (paired) {
605
+ tokens.put("#READ", pair.get("read").key);
606
+ tokens.put("#WRITE", pair.get("write").key);
607
+ if (null != item) {
608
+ tokens.put("#ITEM", item.key);
609
+ }
610
+ if (null != other) {
611
+ tokens.put("#OTHER", other.key);
612
+ }
613
+ }
475
614
  Map<String, Object> kase = (Map<String, Object>) resolve(rawCase, tokens);
476
615
  String label = String.valueOf(kase.get("name"));
477
616
  ran++;
@@ -492,7 +631,7 @@ public class FeatureCorpusTest {
492
631
  Object wanterr = step.get("err");
493
632
 
494
633
  try {
495
- invoke(client, op, ctrl);
634
+ step(client, op, step, ctrl);
496
635
  if (null != wanterr) {
497
636
  fail(label + ": " + step.get("op") + " was expected to fail, and did not");
498
637
  }
@@ -487,6 +487,66 @@ public class FeatureTest {
487
487
  assertEquals(3, rec.calls.size(), "expected 3 calls");
488
488
  }
489
489
 
490
+ @Test
491
+ public void cache_keysOnRequestHeaders() {
492
+ assumeFeatures("cache");
493
+ FhRecorder rec = new FhRecorder();
494
+ FhHarness h = fhMake(rec::fetch, fhF(new CacheFeature(), fhMap("ttl", 10000)));
495
+ h.op(fhOp("load").path("/w/1").headers(fhMap("x-tenant", "tenant-a")));
496
+ h.op(fhOp("load").path("/w/1").headers(fhMap("X-Tenant", "tenant-b")));
497
+ assertEquals(2, rec.calls.size(), "a second tenant was served the first tenant's entry");
498
+ h.op(fhOp("load").path("/w/1").headers(fhMap("x-tenant", "tenant-b")));
499
+ assertEquals(2, rec.calls.size(), "the same headers did not share an entry");
500
+ }
501
+
502
+ @Test
503
+ public void cache_aWriteDropsItsCollection() {
504
+ assumeFeatures("cache");
505
+ FhRecorder rec = new FhRecorder();
506
+ CacheFeature f = new CacheFeature();
507
+ FhHarness h = fhMake(rec::fetch, fhF(f, fhMap("ttl", 10000)));
508
+ String[] paths = {"/w/1", "/w", "/w/1/part", "/other/1", "/w/2"};
509
+ for (String p : paths) {
510
+ h.op(fhOp("load").path(p));
511
+ }
512
+ h.op(fhOp("update").method("PUT").path("/w/1"));
513
+ for (String p : paths) {
514
+ h.op(fhOp("load").path(p));
515
+ }
516
+ assertEquals(9, rec.calls.size(), "expected the collection refetched and /other cached");
517
+ assertEquals(2, f.hit, "expected /other served from cache");
518
+ }
519
+
520
+ @Test
521
+ public void cache_cacheControlPreventsStoring() {
522
+ assumeFeatures("cache");
523
+ for (String control : new String[] {"no-store", "private", "No-Cache", "max-age=60, private"}) {
524
+ FhRecorder rec = new FhRecorder();
525
+ rec.reply = (n, fetchdef) -> fhResponse(200, fhMap("ok", true), fhMap("Cache-Control", control));
526
+ FhHarness h = fhMake(rec::fetch, fhF(new CacheFeature(), fhMap("ttl", 10000)));
527
+ h.op(fhOp("load").path("/w"));
528
+ h.op(fhOp("load").path("/w"));
529
+ assertEquals(2, rec.calls.size(), control + " was stored");
530
+ }
531
+ }
532
+
533
+ @Test
534
+ public void cache_maxAgeCapsTheTtl() {
535
+ assumeFeatures("cache");
536
+ FhClock clock = new FhClock();
537
+ FhRecorder rec = new FhRecorder();
538
+ rec.reply = (n, fetchdef) -> fhResponse(200, fhMap("ok", true), fhMap("cache-control", "max-age=1"));
539
+ FhHarness h = fhMake(rec::fetch, fhF(new CacheFeature(),
540
+ fhMap("ttl", 10000, "now", (LongSupplier) clock::now)));
541
+ h.op(fhOp("load").path("/w"));
542
+ clock.advance(500);
543
+ h.op(fhOp("load").path("/w"));
544
+ assertEquals(1, rec.calls.size(), "expected a hit inside max-age");
545
+ clock.advance(600);
546
+ h.op(fhOp("load").path("/w"));
547
+ assertEquals(2, rec.calls.size(), "expected max-age=1 to expire the entry before the ttl");
548
+ }
549
+
490
550
  @Test
491
551
  public void cache_inactiveDoesNotWrap() {
492
552
  assumeFeatures("cache");
@@ -40,8 +40,8 @@ import JAVAPACKAGE.utility.struct.Struct;
40
40
  // sensitive KEY name is masked whatever it holds. Inside the pipeline data
41
41
  // stays raw, so a hook can still read the header it must add to.
42
42
  //
43
- // The configuration is the derived block MakeOptions builds
44
- // (`options.__derived__.clean`): active, keys, values, mask, hint, min. It
43
+ // The configuration is the registry MakeOptions builds beside the options
44
+ // (`ctx.cleancfg`): active, keys, values, mask, hint, min. It
45
45
  // is a map so the registry can grow after construction - the auth step and
46
46
  // the features register what they handle later.
47
47
  @SuppressWarnings({"unchecked"})
@@ -74,16 +74,12 @@ final class Clean {
74
74
 
75
75
  private Clean() {}
76
76
 
77
- // A context without options (makeError is reached with a bare one) falls
78
- // back to the schema defaults, so nothing leaves raw for want of a
77
+ // A context without a registry (makeError is reached with a bare one)
78
+ // falls back to the schema defaults, so nothing leaves raw for want of a
79
79
  // constructor.
80
80
  static Map<String, Object> cleanConfig(Context ctx) {
81
- Object derived = null;
82
- if (ctx != null && ctx.options != null) {
83
- derived = Struct.getpath(ctx.options, List.of("__derived__", "clean"));
84
- }
85
- if (derived instanceof Map) {
86
- return (Map<String, Object>) derived;
81
+ if (ctx != null && ctx.cleancfg != null) {
82
+ return ctx.cleancfg;
87
83
  }
88
84
  return makeCleanConfig(Helpers.toMapAny(Schema.optspec().get("clean")));
89
85
  }
@@ -226,6 +226,10 @@ public final class Fetcher {
226
226
  catch (RuntimeException e) {
227
227
  throw new RuntimeException("fetch: invalid url: " + fullurl, e);
228
228
  }
229
+ // A bound in milliseconds, for a caller beneath the timeout feature.
230
+ if (fetchdef.get("timeout") instanceof Number && 0 < ((Number) fetchdef.get("timeout")).longValue()) {
231
+ reqb.timeout(java.time.Duration.ofMillis(((Number) fetchdef.get("timeout")).longValue()));
232
+ }
229
233
 
230
234
  boolean hasUA = false;
231
235
  Object headersRaw = fetchdef.get("headers");
@@ -379,7 +383,7 @@ public final class Fetcher {
379
383
  }
380
384
  }
381
385
 
382
- static Object fetcher(Context ctx, String fullurl, Map<String, Object> fetchdef) {
386
+ public static Object fetcher(Context ctx, String fullurl, Map<String, Object> fetchdef) {
383
387
  if (!"live".equals(ctx.client.mode)) {
384
388
  throw ctx.makeError("fetch_mode_block",
385
389
  "Request blocked by mode: \"" + ctx.client.mode
@@ -266,10 +266,12 @@ final class MakeOptions {
266
266
  }
267
267
 
268
268
  Map<String, Object> derived = new LinkedHashMap<>();
269
- derived.put("clean", cleancfg);
270
269
  derived.put("featureorder", featureorder);
271
270
  opts.put("__derived__", derived);
272
271
 
272
+ // Beside the options, not in them: optionsMap hands them to callers.
273
+ ctx.cleancfg = cleancfg;
274
+
273
275
  // Again over the merged result: the config's own defaults can carry one.
274
276
  Clean.addSensitiveOptions(cleancfg, without(opts, "clean", "__derived__"));
275
277