@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
@@ -430,6 +430,79 @@ static void cache_inactiveDoesNotWrap() {
430
430
  ASSERT_EQ((int)rec->calls.size(), 2, "expected 2 calls");
431
431
  }
432
432
 
433
+ // The reference vectors of the SipHash paper: key 00..0f, message 00..len-1.
434
+ static void cache_siphashMatchesTheReferenceVectors() {
435
+ if (!have({"cache"})) return;
436
+ const std::vector<std::pair<size_t, uint64_t>> vectors = {{0, 0x726fdb47dd0e0e31ULL}, {1, 0x74f839c593dc67fdULL}, {2, 0x0d6c8009d9a94f5aULL}, {3, 0x85676696d7fb7e2dULL}, {4, 0xcf2794e0277187b7ULL}, {5, 0x18765564cd99a68dULL}, {6, 0xcbc9466e58fee3ceULL}, {7, 0xab0200f58b01d137ULL}, {8, 0x93f5f5799a932462ULL}, {15, 0xa129ca6149be45e5ULL}, {16, 0x3f2acc7f57c29bdbULL}, {63, 0x958a324ceb064572ULL}};
437
+ uint8_t key[16];
438
+ for (int i = 0; i < 16; i++) key[i] = static_cast<uint8_t>(i);
439
+ std::string msg;
440
+ for (int i = 0; i < 64; i++) msg.push_back(static_cast<char>(i));
441
+ for (const auto& v : vectors) {
442
+ ASSERT_TRUE(CacheFeature::siphash24(key, msg.substr(0, v.first)) == v.second,
443
+ "siphash reference vector " + std::to_string(v.first));
444
+ }
445
+ }
446
+
447
+ static void cache_keysOnRequestHeaders() {
448
+ if (!have({"cache"})) return;
449
+ auto rec = std::make_shared<FhRecorder>();
450
+ FetcherFn server = [rec](CtxPtr c, const std::string& u, const Value& fd) { return rec->fetch(c, u, fd); };
451
+ auto h = fhMake(server, {FF(std::make_shared<CacheFeature>(), fhMap({{"ttl", Value(10000)}}))});
452
+ h->op(fhOp("load").setPath("/w/1").setHeaders(fhMap({{"x-tenant", Value("tenant-a")}})));
453
+ h->op(fhOp("load").setPath("/w/1").setHeaders(fhMap({{"X-Tenant", Value("tenant-b")}})));
454
+ ASSERT_EQ((int)rec->calls.size(), 2, "a second tenant was served the first tenant's entry");
455
+ h->op(fhOp("load").setPath("/w/1").setHeaders(fhMap({{"x-tenant", Value("tenant-b")}})));
456
+ ASSERT_EQ((int)rec->calls.size(), 2, "the same headers did not share an entry");
457
+ }
458
+
459
+ static void cache_aWriteDropsItsCollection() {
460
+ if (!have({"cache"})) return;
461
+ auto rec = std::make_shared<FhRecorder>();
462
+ FetcherFn server = [rec](CtxPtr c, const std::string& u, const Value& fd) { return rec->fetch(c, u, fd); };
463
+ auto f = std::make_shared<CacheFeature>();
464
+ auto h = fhMake(server, {FF(f, fhMap({{"ttl", Value(10000)}}))});
465
+ const char* paths[] = {"/w/1", "/w", "/w/1/part", "/other/1", "/w/2"};
466
+ for (const char* p : paths) h->op(fhOp("load").setPath(p));
467
+ h->op(fhOp("update").setMethod("PUT").setPath("/w/1"));
468
+ for (const char* p : paths) h->op(fhOp("load").setPath(p));
469
+ ASSERT_EQ((int)rec->calls.size(), 9, "expected the collection refetched and /other cached");
470
+ ASSERT_EQ(f->hit, 2, "expected /other served from cache");
471
+ }
472
+
473
+ static void cache_cacheControlPreventsStoring() {
474
+ if (!have({"cache"})) return;
475
+ for (std::string control : {"no-store", "private", "No-Cache", "max-age=60, private"}) {
476
+ auto rec = std::make_shared<FhRecorder>();
477
+ rec->reply = [control](int, const Value&) {
478
+ return fhResponse(200, fhMap({{"ok", Value(true)}}), fhMap({{"Cache-Control", Value(control)}}));
479
+ };
480
+ FetcherFn server = [rec](CtxPtr c, const std::string& u, const Value& fd) { return rec->fetch(c, u, fd); };
481
+ auto h = fhMake(server, {FF(std::make_shared<CacheFeature>(), fhMap({{"ttl", Value(10000)}}))});
482
+ h->op(fhOp("load").setPath("/w"));
483
+ h->op(fhOp("load").setPath("/w"));
484
+ ASSERT_EQ((int)rec->calls.size(), 2, control + " was stored");
485
+ }
486
+ }
487
+
488
+ static void cache_maxAgeCapsTheTtl() {
489
+ if (!have({"cache"})) return;
490
+ FhClock clock;
491
+ auto rec = std::make_shared<FhRecorder>();
492
+ rec->reply = [](int, const Value&) {
493
+ return fhResponse(200, fhMap({{"ok", Value(true)}}), fhMap({{"cache-control", Value("max-age=1")}}));
494
+ };
495
+ FetcherFn server = [rec](CtxPtr c, const std::string& u, const Value& fd) { return rec->fetch(c, u, fd); };
496
+ auto h = fhMake(server, {FF(std::make_shared<CacheFeature>(), fhMap({{"ttl", Value(10000)}, {"now", clock.nowFn()}}))});
497
+ h->op(fhOp("load").setPath("/w"));
498
+ clock.advance(500);
499
+ h->op(fhOp("load").setPath("/w"));
500
+ ASSERT_EQ((int)rec->calls.size(), 1, "expected a hit inside max-age");
501
+ clock.advance(600);
502
+ h->op(fhOp("load").setPath("/w"));
503
+ ASSERT_EQ((int)rec->calls.size(), 2, "expected max-age=1 to expire the entry before the ttl");
504
+ }
505
+
433
506
  // ============================ idempotency ==============================
434
507
 
435
508
  static void idempotency_addsKeyToMutatingOps() {
@@ -700,6 +773,26 @@ static void audit_inactiveRecordsNothing() {
700
773
  ASSERT_EQ((int)f->records.as_list()->size(), 0, "expected no records");
701
774
  }
702
775
 
776
+ static void audit_perCallMarkerEndsWithTheCall() {
777
+ if (!have({"audit", "netsim"})) return;
778
+ auto f = std::make_shared<AuditFeature>();
779
+ auto h = fhMake(nullptr, {
780
+ FF(std::make_shared<NetsimFeature>(), fhMap({{"failEvery", Value(2)}, {"failStatus", Value(500)}})),
781
+ FF(f, Value::undef())});
782
+ std::vector<std::weak_ptr<Context>> held;
783
+ for (int i = 0; i < 20; i++) {
784
+ auto res = h->op(fhOp("load").setPath("/w/" + std::to_string(i)));
785
+ ASSERT_TRUE(nullptr != res.ctx, "expected the call's context");
786
+ if (nullptr == res.ctx) return;
787
+ ASSERT_TRUE(res.ctx->out.extra.count("audit_seen") == 1, "expected the marker on the call's context");
788
+ held.push_back(res.ctx);
789
+ }
790
+ ASSERT_EQ((int)f->records.as_list()->size(), 20, "expected one record per call");
791
+ int alive = 0;
792
+ for (auto& w : held) if (!w.expired()) alive++;
793
+ ASSERT_EQ(alive, 0, "expected no context kept alive past its call");
794
+ }
795
+
703
796
  // ============================ one call, one id =========================
704
797
 
705
798
  static void telemetryDebugAudit_carryOneCallsContextId() {
@@ -962,6 +1055,11 @@ int main() {
962
1055
  T_RUN(cache_refetchesAfterTtl);
963
1056
  T_RUN(cache_evictsOldestPastMax);
964
1057
  T_RUN(cache_inactiveDoesNotWrap);
1058
+ T_RUN(cache_keysOnRequestHeaders);
1059
+ T_RUN(cache_aWriteDropsItsCollection);
1060
+ T_RUN(cache_cacheControlPreventsStoring);
1061
+ T_RUN(cache_maxAgeCapsTheTtl);
1062
+ T_RUN(cache_siphashMatchesTheReferenceVectors);
965
1063
 
966
1064
  T_RUN(idempotency_addsKeyToMutatingOps);
967
1065
  T_RUN(idempotency_addsKeyByHttpMethod);
@@ -994,6 +1092,7 @@ int main() {
994
1092
  T_RUN(audit_defaultActorAnonymous);
995
1093
  T_RUN(audit_injectedClock);
996
1094
  T_RUN(audit_inactiveRecordsNothing);
1095
+ T_RUN(audit_perCallMarkerEndsWithTheCall);
997
1096
 
998
1097
  T_RUN(telemetryDebugAudit_carryOneCallsContextId);
999
1098
 
@@ -67,9 +67,10 @@ inline CtxPtr makeContext(const CtxSpec& cs, CtxPtr basectx) {
67
67
  // sensitive KEY name is masked whatever it holds. Inside the pipeline data
68
68
  // stays raw, so a hook can still read the header it must add to.
69
69
  //
70
- // The configuration is the derived clean block makeOptions builds
71
- // (`options.__derived__.clean`): a plain map, so the registry list inside
72
- // it can be replaced after construction - features register later.
70
+ // The configuration is the clean registry makeOptions builds and holds on
71
+ // the context beside the options (`ctx->cleancfg`): a plain map, so the
72
+ // registry list inside it can be replaced after construction - features
73
+ // register later.
73
74
 
74
75
  constexpr int CLEAN_MAXDEPTH = 32;
75
76
 
@@ -159,12 +160,11 @@ inline Value makeCleanConfig(const Value& cleanopts) {
159
160
  return cfg;
160
161
  }
161
162
 
162
- // A context without options (makeError is reached with a bare one) falls
163
+ // A context without a registry (makeError is reached with a bare one) falls
163
164
  // back to the schema defaults, so nothing leaves raw for want of a
164
165
  // constructor.
165
166
  inline Value cleanConfig(CtxPtr ctx) {
166
- Value derived = ctx ? Struct::getpath(ctx->options, {"__derived__", "clean"}) : Value::undef();
167
- if (derived.is_map()) return derived;
167
+ if (ctx && ctx->cleancfg.is_map()) return ctx->cleancfg;
168
168
  return makeCleanConfig(getp(sharedOptspec(), "clean"));
169
169
  }
170
170
 
@@ -2260,10 +2260,12 @@ inline Value makeOptions(CtxPtr ctx) {
2260
2260
  for (const auto& n : featureOrder) orderList.as_list()->push_back(Value(n));
2261
2261
 
2262
2262
  Value derived = vmap();
2263
- map_put(derived, "clean", derivedClean);
2264
2263
  map_put(derived, "featureorder", orderList);
2265
2264
  map_put(opts, "__derived__", derived);
2266
2265
 
2266
+ // Beside the options, not in them: optionsMap hands them to callers.
2267
+ ctx->cleancfg = derivedClean;
2268
+
2267
2269
  // Again over the merged result: the config's own defaults can carry one.
2268
2270
  cleanAddSensitiveCfg(derivedClean, optsWithout(opts, {"clean", "__derived__"}));
2269
2271
 
@@ -19,6 +19,8 @@ public class Context
19
19
  public Dictionary<string, object?>? Config;
20
20
  public Dictionary<string, object?>? Entopts;
21
21
  public Dictionary<string, object?>? Options;
22
+ // The clean registry, beside the options rather than in them.
23
+ public ProjectNameSdk.Util.CleanConfig? CleanCfg;
22
24
  public ConcurrentDictionary<string, Operation> Opmap = new();
23
25
  public Response? Response;
24
26
  public Result? Result;
@@ -125,6 +127,8 @@ public class Context
125
127
  {
126
128
  Options = basectx.Options;
127
129
  }
130
+ CleanCfg = Helpers.GetCtxProp(ctxmap, "cleancfg") as ProjectNameSdk.Util.CleanConfig
131
+ ?? basectx?.CleanCfg;
128
132
 
129
133
  // Entity
130
134
  if (Helpers.GetCtxProp(ctxmap, "entity") is IEntity ent)
@@ -1,11 +1,9 @@
1
- // Response caching for safe (read) requests. Wraps the active transport and
2
- // serves a fresh cached snapshot instead of hitting the network when the
3
- // same method+URL was fetched within `ttl` ms (default: 5000). Only
4
- // successful (2xx) responses to cacheable methods (default: GET) are
5
- // stored, keyed by method+URL. The cache is bounded (`max` entries, default
6
- // 256, oldest evicted first) and every hit/miss/bypass is counted. Bodies
7
- // are snapshotted on capture so both the current caller and later hits can
8
- // re-read the JSON body repeatedly.
1
+ // Response caching for safe (read) requests, per client: see the features
2
+ // reference for the key, the write invalidation and the Cache-Control rules.
3
+
4
+ using System.Security.Cryptography;
5
+ using System.Text;
6
+ using System.Text.RegularExpressions;
9
7
 
10
8
  using ProjectNameSdk.Util;
11
9
 
@@ -19,6 +17,11 @@ public class CacheFeature : BaseFeature
19
17
  private Dictionary<string, object?>? _options;
20
18
  private Dictionary<string, CacheEntry> _store = new();
21
19
  private List<string> _order = new();
20
+ private byte[] _secret = Array.Empty<byte>();
21
+
22
+ private static readonly string[] SafeMethods = { "GET", "HEAD" };
23
+ private static readonly string[] Unstored = { "no-store", "private", "no-cache" };
24
+ private static readonly Regex MaxAge = new("^max-age\\s*=\\s*\"?(\\d+)\"?$");
22
25
 
23
26
  // Activity tracking (mirrors the ts client._cache record).
24
27
  public int Hit;
@@ -28,6 +31,7 @@ public class CacheFeature : BaseFeature
28
31
  private class CacheEntry
29
32
  {
30
33
  public long Expiry;
34
+ public string Scope = "";
31
35
  public CacheSnapshot Snapshot = new();
32
36
  }
33
37
 
@@ -59,6 +63,7 @@ public class CacheFeature : BaseFeature
59
63
 
60
64
  _store = new Dictionary<string, CacheEntry>();
61
65
  _order = new List<string>();
66
+ _secret = RandomNumberGenerator.GetBytes(32);
62
67
 
63
68
  var inner = ctx.Utility!.Fetcher;
64
69
 
@@ -78,10 +83,22 @@ public class CacheFeature : BaseFeature
78
83
  var cacheable = methods.Any(x => x.ToUpperInvariant() == method);
79
84
  if (!cacheable)
80
85
  {
81
- return inner(ctx, url, fetchdef);
86
+ if (SafeMethods.Contains(method))
87
+ {
88
+ return inner(ctx, url, fetchdef);
89
+ }
90
+ try
91
+ {
92
+ return inner(ctx, url, fetchdef);
93
+ }
94
+ finally
95
+ {
96
+ Invalidate(url);
97
+ }
82
98
  }
83
99
 
84
- var key = method + " " + url;
100
+ fetchdef.TryGetValue("headers", out var headers);
101
+ var key = method + " " + url + " " + Digest(headers);
85
102
  var now = FoptNow(_options)();
86
103
 
87
104
  if (_store.TryGetValue(key, out var hit) && hit.Expiry > now)
@@ -111,9 +128,15 @@ public class CacheFeature : BaseFeature
111
128
  if (err == null && Storable(res))
112
129
  {
113
130
  var snapshot = Snapshot(res);
114
- var ttl = FoptInt(_options, "ttl", 5000);
131
+ var ttl = Lifetime(snapshot.Headers, FoptInt(_options, "ttl", 5000));
132
+ if (ttl <= 0)
133
+ {
134
+ Bypass++;
135
+ return Replay(snapshot);
136
+ }
137
+ Drop(key);
115
138
  Evict();
116
- _store[key] = new CacheEntry { Expiry = now + ttl, Snapshot = snapshot };
139
+ _store[key] = new CacheEntry { Expiry = now + ttl, Scope = UrlScope(url), Snapshot = snapshot };
117
140
  _order.Add(key);
118
141
  Miss++;
119
142
  return Replay(snapshot);
@@ -127,6 +150,97 @@ public class CacheFeature : BaseFeature
127
150
  return res;
128
151
  }
129
152
 
153
+ private string Digest(object? headers)
154
+ {
155
+ var pairs = new List<(string Name, string Value)>();
156
+ if (headers is IDictionary<string, object?> hm)
157
+ {
158
+ foreach (var kv in hm)
159
+ {
160
+ if (kv.Value != null)
161
+ {
162
+ pairs.Add((kv.Key.ToLowerInvariant(), Convert.ToString(kv.Value, System.Globalization.CultureInfo.InvariantCulture) ?? ""));
163
+ }
164
+ }
165
+ }
166
+ var text = new StringBuilder();
167
+ foreach (var (name, value) in pairs.OrderBy(p => p.Name, StringComparer.Ordinal).ThenBy(p => p.Value, StringComparer.Ordinal))
168
+ {
169
+ text.Append(name).Append(':').Append(value).Append('\n');
170
+ }
171
+ return Convert.ToHexString(HMACSHA256.HashData(_secret, Encoding.UTF8.GetBytes(text.ToString()))).ToLowerInvariant();
172
+ }
173
+
174
+ private void Invalidate(string url)
175
+ {
176
+ var path = UrlScope(url);
177
+ var collection = UrlParent(path);
178
+ foreach (var key in _store.Where(kv => Under(kv.Value.Scope, path) || kv.Value.Scope == collection)
179
+ .Select(kv => kv.Key).ToList())
180
+ {
181
+ Drop(key);
182
+ }
183
+ }
184
+
185
+ private void Drop(string key)
186
+ {
187
+ if (_store.Remove(key))
188
+ {
189
+ _order.Remove(key);
190
+ }
191
+ }
192
+
193
+ // The configured ttl capped by the response's Cache-Control; zero when the
194
+ // response may not be stored.
195
+ private static long Lifetime(Dictionary<string, object?> headers, long ttl)
196
+ {
197
+ var control = "";
198
+ foreach (var kv in headers)
199
+ {
200
+ if (kv.Key.ToLowerInvariant() == "cache-control")
201
+ {
202
+ control = (Convert.ToString(kv.Value) ?? "").ToLowerInvariant();
203
+ }
204
+ }
205
+ var life = ttl;
206
+ foreach (var part in control.Split(','))
207
+ {
208
+ var directive = part.Trim();
209
+ if (Unstored.Contains(directive))
210
+ {
211
+ return 0;
212
+ }
213
+ var age = MaxAge.Match(directive);
214
+ if (age.Success && long.TryParse(age.Groups[1].Value, out var secs))
215
+ {
216
+ life = Math.Min(life, secs * 1000);
217
+ }
218
+ }
219
+ return life;
220
+ }
221
+
222
+ private static string UrlScope(string url)
223
+ {
224
+ var cut = url.IndexOfAny(new[] { '?', '#' });
225
+ return (cut < 0 ? url : url[..cut]).TrimEnd('/');
226
+ }
227
+
228
+ private static string UrlParent(string scope)
229
+ {
230
+ var scheme = scope.IndexOf("://", StringComparison.Ordinal);
231
+ var root = scheme < 0 ? 0 : scope.IndexOf('/', scheme + 3);
232
+ if (root < 0)
233
+ {
234
+ return scope;
235
+ }
236
+ return scope[..Math.Max(scope.LastIndexOf('/'), root)];
237
+ }
238
+
239
+ private static bool Under(string scope, string prefix)
240
+ {
241
+ return scope == prefix || scope.StartsWith(prefix + "/", StringComparison.Ordinal);
242
+ }
243
+
130
244
  // A body the transport could not read as JSON passes through, for the operation to report.
131
245
  private static bool Storable(object? res)
132
246
  {
@@ -87,6 +87,7 @@ namespace ProjectNameSdk.Feature;
87
87
  public class SecretsFeature : BaseFeature
88
88
  {
89
89
  private ProjectNameSDK? _client;
90
+ private Context? _rootctx;
90
91
 
91
92
  // The LIVE options map (root ctx options). READ ONLY: this feature
92
93
  // never writes it (see the header).
@@ -135,6 +136,7 @@ public class SecretsFeature : BaseFeature
135
136
  public override void Init(Context ctx, Dictionary<string, object?> options)
136
137
  {
137
138
  _client = ctx.Client;
139
+ _rootctx = ctx;
138
140
  _liveopts = ctx.Options;
139
141
  Active = FoptBool(options, "active", false);
140
142
 
@@ -785,35 +787,15 @@ public class SecretsFeature : BaseFeature
785
787
  ["body"] = body,
786
788
  // Never followed by the client: a redirect would carry the refresh token on.
787
789
  ["redirect"] = "manual",
790
+ // Its own bound, in milliseconds: the timeout feature's wrapper is above it.
791
+ ["timeout"] = 30000,
788
792
  };
789
793
 
790
- // Deliberately NOT the SDK transport. The transport is what this
791
- // feature wraps, and sending the token request back through it
792
- // would recurse on the first expiry - and would route the exchange
793
- // through the test mock, which knows nothing about it.
794
- //
795
- // system.fetch when the caller supplied one, else SdkUtility's own
796
- // raw HttpClient fetch. The fallback matters: MakeOptions leaves
797
- // system.fetch unset in the ORDINARY case, and requiring a custom
798
- // transport there would reject every live token purchase before a
799
- // request was made. DefaultHttpFetch is `internal` in this
800
- // assembly, is not the SDK transport (no feature wrapping, no
801
- // mode/test block) and returns the same { status, json } shape the
802
- // system.fetch seam promises.
803
- var sysfetch = StructUtils.GetPath(options, StructUtils.Jt("system", "fetch"));
804
-
805
- object? Fetch(string at)
806
- {
807
- if (sysfetch is Func<string, Dictionary<string, object?>, Dictionary<string, object?>> ff)
808
- {
809
- return ff(at, fetchdef);
810
- }
811
- if (sysfetch is Func<string, Dictionary<string, object?>, object?> ffa)
812
- {
813
- return ffa(at, fetchdef);
814
- }
815
- return SdkUtility.DefaultHttpFetch(at, fetchdef);
816
- }
794
+ // The SDK's own transport, beneath every feature's wrapper: it holds
795
+ // the test feature's block and the GET-only guard, and the wrapped
796
+ // one would recurse on the first expiry.
797
+ var rootctx = _rootctx!;
798
+ object? Fetch(string at) => SdkUtility.FetcherUtil(rootctx, at, fetchdef);
817
799
 
818
800
  var res = Fetch(url);
819
801
 
@@ -572,6 +572,82 @@ public class FeatureCacheTest
572
572
  Assert.Equal(2, f.Bypass);
573
573
  }
574
574
 
575
+ [Fact]
576
+ public void KeysOnRequestHeaders()
577
+ {
578
+ if (Fh.SkipWithout("cache")) return;
579
+ var rec = new FhRecorder();
580
+ var h = Fh.Make(rec.Fetch, (new CacheFeature(), new Dictionary<string, object?> { ["ttl"] = 10000 }));
581
+ h.Op(new FhOpSpec { Op = "load", Path = "/w/1", Headers = new() { ["x-tenant"] = "tenant-a" } });
582
+ h.Op(new FhOpSpec { Op = "load", Path = "/w/1", Headers = new() { ["X-Tenant"] = "tenant-b" } });
583
+ Assert.Equal(2, rec.Calls.Count);
584
+ h.Op(new FhOpSpec { Op = "load", Path = "/w/1", Headers = new() { ["x-tenant"] = "tenant-b" } });
585
+ Assert.Equal(2, rec.Calls.Count);
586
+ }
587
+
588
+ [Fact]
589
+ public void AWriteDropsItsCollection()
590
+ {
591
+ if (Fh.SkipWithout("cache")) return;
592
+ var rec = new FhRecorder();
593
+ var f = new CacheFeature();
594
+ var h = Fh.Make(rec.Fetch, (f, new Dictionary<string, object?> { ["ttl"] = 10000 }));
595
+ var paths = new[] { "/w/1", "/w", "/w/1/part", "/other/1", "/w/2" };
596
+ foreach (var p in paths)
597
+ {
598
+ h.Op(new FhOpSpec { Op = "load", Path = p });
599
+ }
600
+ h.Op(new FhOpSpec { Op = "update", Method = "PUT", Path = "/w/1" });
601
+ foreach (var p in paths)
602
+ {
603
+ h.Op(new FhOpSpec { Op = "load", Path = p });
604
+ }
605
+ Assert.Equal(9, rec.Calls.Count);
606
+ Assert.Equal(2, f.Hit);
607
+ }
608
+
609
+ [Fact]
610
+ public void CacheControlPreventsStoring()
611
+ {
612
+ if (Fh.SkipWithout("cache")) return;
613
+ foreach (var control in new[] { "no-store", "private", "No-Cache", "max-age=60, private" })
614
+ {
615
+ var rec = new FhRecorder
616
+ {
617
+ Reply = (_, _) => Fh.Response(200, new Dictionary<string, object?> { ["ok"] = true },
618
+ new Dictionary<string, object?> { ["Cache-Control"] = control }),
619
+ };
620
+ var h = Fh.Make(rec.Fetch, (new CacheFeature(), new Dictionary<string, object?> { ["ttl"] = 10000 }));
621
+ h.Op(new FhOpSpec { Op = "load", Path = "/w" });
622
+ h.Op(new FhOpSpec { Op = "load", Path = "/w" });
623
+ Assert.True(2 == rec.Calls.Count, control + " was stored");
624
+ }
625
+ }
626
+
627
+ [Fact]
628
+ public void MaxAgeCapsTheTtl()
629
+ {
630
+ if (Fh.SkipWithout("cache")) return;
631
+ var clock = new FhClock();
632
+ var rec = new FhRecorder
633
+ {
634
+ Reply = (_, _) => Fh.Response(200, new Dictionary<string, object?> { ["ok"] = true },
635
+ new Dictionary<string, object?> { ["cache-control"] = "max-age=1" }),
636
+ };
637
+ var h = Fh.Make(rec.Fetch, (new CacheFeature(), new Dictionary<string, object?>
638
+ {
639
+ ["ttl"] = 10000,
640
+ ["now"] = (Func<long>)clock.Now,
641
+ }));
642
+ h.Op(new FhOpSpec { Op = "load", Path = "/w" });
643
+ clock.Advance(500);
644
+ h.Op(new FhOpSpec { Op = "load", Path = "/w" });
645
+ Assert.Single(rec.Calls);
646
+ clock.Advance(600);
647
+ h.Op(new FhOpSpec { Op = "load", Path = "/w" });
648
+ Assert.Equal(2, rec.Calls.Count);
649
+ }
650
+
575
651
  [Fact]
576
652
  public void InactiveDoesNotWrap()
577
653
  {
@@ -13,10 +13,9 @@ using Voxgig.Struct;
13
13
 
14
14
  namespace ProjectNameSdk.Util;
15
15
 
16
- // The derived clean block, MUTABLE after MakeOptions since features register
16
+ // The clean registry, MUTABLE after MakeOptions since features register
17
17
  // values later. Fields rather than properties: System.Text.Json serialises
18
- // properties, so a JSON of the options shows this block as {} and never the
19
- // registry it holds.
18
+ // properties, so a JSON of a context never shows the registry it holds.
20
19
  public sealed class CleanConfig
21
20
  {
22
21
  public bool Active = true;
@@ -119,15 +118,11 @@ public static partial class SdkUtility
119
118
  };
120
119
  }
121
120
 
122
- // A context without options (MakeError accepts a bare one) still masks by
123
- // the schema defaults.
121
+ // A context without a registry (MakeError accepts a bare one) still masks
122
+ // by the schema defaults.
124
123
  internal static CleanConfig CleanConfigOf(Context? ctx)
125
124
  {
126
- if (ctx?.Options != null &&
127
- ctx.Options.TryGetValue("__derived__", out var draw) &&
128
- draw is Dictionary<string, object?> derived &&
129
- derived.TryGetValue("clean", out var craw) &&
130
- craw is CleanConfig cfg)
125
+ if (ctx?.CleanCfg is CleanConfig cfg)
131
126
  {
132
127
  return cfg;
133
128
  }
@@ -239,7 +239,13 @@ public static partial class SdkUtility
239
239
  }
240
240
  }
241
241
 
242
- using var resp = ClientFor(fetchdef).Send(req, HttpCompletionOption.ResponseContentRead);
242
+ // A bound in milliseconds, for a caller beneath the timeout feature.
243
+ using var bound = new System.Threading.CancellationTokenSource();
244
+ if (Helpers.ToInt(fetchdef.GetValueOrDefault("timeout")) is var ms && 0 < ms)
245
+ {
246
+ bound.CancelAfter(ms);
247
+ }
248
+ using var resp = ClientFor(fetchdef).Send(req, HttpCompletionOption.ResponseContentRead, bound.Token);
243
249
 
244
250
  var bodyText = resp.Content.ReadAsStringAsync().GetAwaiter().GetResult();
245
251
 
@@ -154,13 +154,7 @@ public static partial class SdkUtility
154
154
  cleanlayers.Add(cleanraw);
155
155
  }
156
156
  var cleancfg = MakeCleanConfig(StructUtils.Merge(cleanlayers));
157
- var cleanctx = new Context(new Dictionary<string, object?>
158
- {
159
- ["options"] = new Dictionary<string, object?>
160
- {
161
- ["__derived__"] = new Dictionary<string, object?> { ["clean"] = cleancfg },
162
- },
163
- }, null);
157
+ var cleanctx = new Context(new Dictionary<string, object?> { ["cleancfg"] = cleancfg }, null);
164
158
  CleanAddOptions(cleanctx, opts, "clean");
165
159
  foreach (var raw in CleanSplitValues(
166
160
  StructUtils.GetPath(cfgopts, StructUtils.Jt("clean", "values")))
@@ -352,11 +346,10 @@ public static partial class SdkUtility
352
346
  }
353
347
  }
354
348
 
355
- opts["__derived__"] = new Dictionary<string, object?>
356
- {
357
- ["clean"] = cleancfg,
358
- ["featureorder"] = featureorder,
359
- };
349
+ opts["__derived__"] = new Dictionary<string, object?> { ["featureorder"] = featureorder };
350
+
351
+ // Beside the options, not in them: OptionsMap hands them to callers.
352
+ ctx.CleanCfg = cleancfg;
360
353
 
361
354
  // Again over the merged result: the config's own defaults can carry one.
362
355
  CleanAddOptions(cleanctx, opts, "clean", "__derived__");
@@ -94,6 +94,8 @@ defmodule ProjectName.Context do
94
94
  S.setprop(ctx, "config", inh_map.("config"))
95
95
  S.setprop(ctx, "entopts", inh_map.("entopts"))
96
96
  S.setprop(ctx, "options", inh_map.("options"))
97
+ # The clean registry, beside the options rather than in them.
98
+ S.setprop(ctx, "cleancfg", inh_map.("cleancfg"))
97
99
  S.setprop(ctx, "entity", inh.("entity"))
98
100
  S.setprop(ctx, "shared", inh_map.("shared"))
99
101