@voxgig/sdkgen 4.49.1 → 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 (218) 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/debug.c +1 -0
  30. package/project/.sdk/tm/c/feature/secrets.c +27 -38
  31. package/project/.sdk/tm/c/feature/telemetry.c +1 -0
  32. package/project/.sdk/tm/c/tests/feature_test.c +142 -0
  33. package/project/.sdk/tm/c/utility/clean.c +26 -33
  34. package/project/.sdk/tm/c/utility/make_options.c +9 -8
  35. package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +14 -11
  36. package/project/.sdk/tm/clojure/src/sdk/client.clj +3 -1
  37. package/project/.sdk/tm/clojure/src/sdk/core.clj +19 -11
  38. package/project/.sdk/tm/clojure/src/sdk/features.clj +83 -10
  39. package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +47 -0
  40. package/project/.sdk/tm/clojure/test/sdk/test/pipeline.clj +1 -0
  41. package/project/.sdk/tm/cpp/Makefile +5 -0
  42. package/project/.sdk/tm/cpp/core/types.hpp +11 -4
  43. package/project/.sdk/tm/cpp/feature/audit.hpp +6 -7
  44. package/project/.sdk/tm/cpp/feature/cache.hpp +188 -9
  45. package/project/.sdk/tm/cpp/feature/debug.hpp +1 -0
  46. package/project/.sdk/tm/cpp/feature/secrets.hpp +12 -0
  47. package/project/.sdk/tm/cpp/feature/telemetry.hpp +1 -0
  48. package/project/.sdk/tm/cpp/test/feature_test.cpp +128 -0
  49. package/project/.sdk/tm/cpp/utility/pipeline.hpp +9 -7
  50. package/project/.sdk/tm/csharp/core/Context.cs +4 -0
  51. package/project/.sdk/tm/csharp/feature/CacheFeature.cs +126 -12
  52. package/project/.sdk/tm/csharp/feature/DebugFeature.cs +1 -0
  53. package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +9 -27
  54. package/project/.sdk/tm/csharp/feature/TelemetryFeature.cs +1 -0
  55. package/project/.sdk/tm/csharp/test/FeatureTest.cs +101 -0
  56. package/project/.sdk/tm/csharp/utility/Clean.cs +5 -10
  57. package/project/.sdk/tm/csharp/utility/Fetcher.cs +7 -1
  58. package/project/.sdk/tm/csharp/utility/MakeOptions.cs +5 -12
  59. package/project/.sdk/tm/elixir/lib/projectname/context.ex +2 -0
  60. package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +146 -30
  61. package/project/.sdk/tm/elixir/lib/projectname/feature/debug.ex +1 -0
  62. package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +10 -56
  63. package/project/.sdk/tm/elixir/lib/projectname/feature/telemetry.ex +1 -0
  64. package/project/.sdk/tm/elixir/lib/projectname/utility.ex +18 -12
  65. package/project/.sdk/tm/elixir/test/feature_test.exs +84 -0
  66. package/project/.sdk/tm/go/core/context.go +6 -0
  67. package/project/.sdk/tm/go/feature/cache_feature.go +131 -4
  68. package/project/.sdk/tm/go/feature/debug_feature.go +3 -2
  69. package/project/.sdk/tm/go/feature/secrets_feature.go +17 -71
  70. package/project/.sdk/tm/go/feature/telemetry_feature.go +5 -4
  71. package/project/.sdk/tm/go/test/feature_corpus_test.go +150 -2
  72. package/project/.sdk/tm/go/test/feature_test.go +101 -0
  73. package/project/.sdk/tm/go/utility/clean.go +5 -7
  74. package/project/.sdk/tm/go/utility/fetcher.go +11 -0
  75. package/project/.sdk/tm/go/utility/make_options.go +5 -7
  76. package/project/.sdk/tm/java/core/Context.java +9 -0
  77. package/project/.sdk/tm/java/feature/CacheFeature.java +123 -17
  78. package/project/.sdk/tm/java/feature/DebugFeature.java +1 -0
  79. package/project/.sdk/tm/java/feature/SecretsFeature.java +9 -92
  80. package/project/.sdk/tm/java/feature/TelemetryFeature.java +1 -0
  81. package/project/.sdk/tm/java/test/FeatureCorpusTest.java +141 -2
  82. package/project/.sdk/tm/java/test/FeatureTest.java +88 -0
  83. package/project/.sdk/tm/java/utility/Clean.java +6 -10
  84. package/project/.sdk/tm/java/utility/Fetcher.java +5 -1
  85. package/project/.sdk/tm/java/utility/MakeOptions.java +3 -1
  86. package/project/.sdk/tm/js/src/Context.js +4 -0
  87. package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +118 -11
  88. package/project/.sdk/tm/js/src/feature/cost/CostFeature.js +3 -2
  89. package/project/.sdk/tm/js/src/feature/debug/DebugFeature.js +1 -0
  90. package/project/.sdk/tm/js/src/feature/metrics/MetricsFeature.js +1 -1
  91. package/project/.sdk/tm/js/src/feature/paging/PagingFeature.js +4 -4
  92. package/project/.sdk/tm/js/src/feature/rbac/RbacFeature.js +4 -4
  93. package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +16 -6
  94. package/project/.sdk/tm/js/src/feature/telemetry/TelemetryFeature.js +1 -0
  95. package/project/.sdk/tm/js/src/feature/validate/ValidateFeature.js +2 -1
  96. package/project/.sdk/tm/js/src/utility/CleanUtility.js +1 -1
  97. package/project/.sdk/tm/js/src/utility/MakeOptionsUtility.js +6 -6
  98. package/project/.sdk/tm/js/test/feature/Corpus.test.js +100 -2
  99. package/project/.sdk/tm/js/test/feature/harness.js +3 -3
  100. package/project/.sdk/tm/js/test/feature.test.js +17 -0
  101. package/project/.sdk/tm/js/test/pipeline.test.js +1 -1
  102. package/project/.sdk/tm/kotlin/core/Context.kt +3 -0
  103. package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +102 -7
  104. package/project/.sdk/tm/kotlin/feature/DebugFeature.kt +1 -0
  105. package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +10 -20
  106. package/project/.sdk/tm/kotlin/feature/TelemetryFeature.kt +1 -0
  107. package/project/.sdk/tm/kotlin/test/FeatureTest.kt +79 -0
  108. package/project/.sdk/tm/kotlin/utility/CoreUtil.kt +5 -5
  109. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +4 -0
  110. package/project/.sdk/tm/kotlin/utility/MakeOptions.kt +3 -1
  111. package/project/.sdk/tm/lua/core/context.lua +6 -0
  112. package/project/.sdk/tm/lua/feature/cache_feature.lua +212 -15
  113. package/project/.sdk/tm/lua/feature/debug_feature.lua +1 -0
  114. package/project/.sdk/tm/lua/feature/secrets_feature.lua +15 -80
  115. package/project/.sdk/tm/lua/feature/telemetry_feature.lua +1 -0
  116. package/project/.sdk/tm/lua/test/feature_test.lua +108 -0
  117. package/project/.sdk/tm/lua/utility/clean.lua +6 -9
  118. package/project/.sdk/tm/lua/utility/fetcher.lua +9 -0
  119. package/project/.sdk/tm/lua/utility/make_options.lua +5 -5
  120. package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +21 -6
  121. package/project/.sdk/tm/ocaml/sdk_features.ml +108 -8
  122. package/project/.sdk/tm/ocaml/sdk_runtime.ml +13 -10
  123. package/project/.sdk/tm/ocaml/sdk_types.ml +2 -0
  124. package/project/.sdk/tm/ocaml/test/t_feature.ml +51 -1
  125. package/project/.sdk/tm/perl/core/context.pm +3 -0
  126. package/project/.sdk/tm/perl/feature/audit_feature.pm +4 -7
  127. package/project/.sdk/tm/perl/feature/cache_feature.pm +111 -15
  128. package/project/.sdk/tm/perl/feature/debug_feature.pm +1 -0
  129. package/project/.sdk/tm/perl/feature/secrets_feature.pm +13 -18
  130. package/project/.sdk/tm/perl/feature/telemetry_feature.pm +1 -0
  131. package/project/.sdk/tm/perl/t/feature.t +86 -0
  132. package/project/.sdk/tm/perl/t/feature_corpus.t +80 -4
  133. package/project/.sdk/tm/perl/utility/clean.pm +6 -8
  134. package/project/.sdk/tm/perl/utility/fetcher.pm +4 -0
  135. package/project/.sdk/tm/perl/utility/make_options.pm +5 -5
  136. package/project/.sdk/tm/php/core/Context.php +4 -0
  137. package/project/.sdk/tm/php/feature/CacheFeature.php +98 -13
  138. package/project/.sdk/tm/php/feature/DebugFeature.php +1 -0
  139. package/project/.sdk/tm/php/feature/SecretsFeature.php +18 -15
  140. package/project/.sdk/tm/php/feature/TelemetryFeature.php +1 -0
  141. package/project/.sdk/tm/php/test/FeatureCorpusTest.php +113 -3
  142. package/project/.sdk/tm/php/test/FeatureTest.php +90 -0
  143. package/project/.sdk/tm/php/utility/Clean.php +3 -10
  144. package/project/.sdk/tm/php/utility/Fetcher.php +8 -3
  145. package/project/.sdk/tm/php/utility/MakeOptions.php +5 -7
  146. package/project/.sdk/tm/py/pkg/core/context.py +4 -0
  147. package/project/.sdk/tm/py/pkg/feature/cache_feature.py +79 -13
  148. package/project/.sdk/tm/py/pkg/feature/debug_feature.py +1 -0
  149. package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +15 -66
  150. package/project/.sdk/tm/py/pkg/feature/telemetry_feature.py +1 -0
  151. package/project/.sdk/tm/py/pkg/utility/clean.py +4 -10
  152. package/project/.sdk/tm/py/pkg/utility/make_options.py +8 -9
  153. package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +23 -9
  154. package/project/.sdk/tm/py/test/test_feature.py +73 -0
  155. package/project/.sdk/tm/py/test/test_feature_corpus.py +88 -3
  156. package/project/.sdk/tm/rb/core/context.rb +4 -1
  157. package/project/.sdk/tm/rb/feature/audit_feature.rb +6 -5
  158. package/project/.sdk/tm/rb/feature/cache_feature.rb +75 -11
  159. package/project/.sdk/tm/rb/feature/debug_feature.rb +1 -0
  160. package/project/.sdk/tm/rb/feature/secrets_feature.rb +9 -64
  161. package/project/.sdk/tm/rb/feature/telemetry_feature.rb +1 -0
  162. package/project/.sdk/tm/rb/test/feature_corpus_test.rb +85 -3
  163. package/project/.sdk/tm/rb/test/feature_test.rb +93 -0
  164. package/project/.sdk/tm/rb/utility/clean.rb +6 -7
  165. package/project/.sdk/tm/rb/utility/fetcher.rb +6 -0
  166. package/project/.sdk/tm/rb/utility/make_options.rb +5 -5
  167. package/project/.sdk/tm/rust/core/context.rs +8 -0
  168. package/project/.sdk/tm/rust/feature/cache.rs +120 -3
  169. package/project/.sdk/tm/rust/feature/debug.rs +1 -0
  170. package/project/.sdk/tm/rust/feature/secrets.rs +36 -98
  171. package/project/.sdk/tm/rust/feature/telemetry.rs +1 -0
  172. package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +3 -3
  173. package/project/.sdk/tm/rust/tests/feature_test.rs +141 -0
  174. package/project/.sdk/tm/rust/utility/clean.rs +35 -43
  175. package/project/.sdk/tm/rust/utility/fetcher.rs +6 -0
  176. package/project/.sdk/tm/rust/utility/make_options.rs +8 -11
  177. package/project/.sdk/tm/scala/core/Context.scala +4 -0
  178. package/project/.sdk/tm/scala/feature/CacheFeature.scala +117 -11
  179. package/project/.sdk/tm/scala/feature/DebugFeature.scala +1 -0
  180. package/project/.sdk/tm/scala/feature/SecretsFeature.scala +11 -24
  181. package/project/.sdk/tm/scala/feature/TelemetryFeature.scala +1 -0
  182. package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +52 -1
  183. package/project/.sdk/tm/scala/utility/Fetcher.scala +5 -0
  184. package/project/.sdk/tm/scala/utility/Make.scala +3 -1
  185. package/project/.sdk/tm/scala/utility/Misc.scala +5 -8
  186. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Context.swift +3 -0
  187. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +122 -11
  188. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/DebugFeature.swift +1 -0
  189. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +9 -13
  190. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/TelemetryFeature.swift +1 -0
  191. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Fetcher.swift +4 -0
  192. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/MakeOptions.swift +5 -4
  193. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/ResultUtil.swift +4 -5
  194. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +79 -0
  195. package/project/.sdk/tm/ts/src/Context.ts +4 -0
  196. package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +115 -4
  197. package/project/.sdk/tm/ts/src/feature/cost/CostFeature.ts +3 -2
  198. package/project/.sdk/tm/ts/src/feature/debug/DebugFeature.ts +1 -0
  199. package/project/.sdk/tm/ts/src/feature/metrics/MetricsFeature.ts +1 -1
  200. package/project/.sdk/tm/ts/src/feature/paging/PagingFeature.ts +4 -4
  201. package/project/.sdk/tm/ts/src/feature/rbac/RbacFeature.ts +4 -4
  202. package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +14 -2
  203. package/project/.sdk/tm/ts/src/feature/telemetry/TelemetryFeature.ts +1 -0
  204. package/project/.sdk/tm/ts/src/feature/validate/ValidateFeature.ts +2 -1
  205. package/project/.sdk/tm/ts/src/utility/CleanUtility.ts +1 -1
  206. package/project/.sdk/tm/ts/src/utility/MakeOptionsUtility.ts +6 -6
  207. package/project/.sdk/tm/ts/test/feature/Corpus.test.ts +101 -2
  208. package/project/.sdk/tm/ts/test/feature/harness.ts +3 -3
  209. package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
  210. package/project/.sdk/tm/ts/test/pipeline.test.ts +1 -1
  211. package/project/.sdk/tm/zig/core/context.zig +5 -0
  212. package/project/.sdk/tm/zig/core/utility.zig +11 -9
  213. package/project/.sdk/tm/zig/feature/cache.zig +144 -13
  214. package/project/.sdk/tm/zig/feature/debug.zig +1 -0
  215. package/project/.sdk/tm/zig/feature/secrets.zig +6 -1
  216. package/project/.sdk/tm/zig/feature/telemetry.zig +1 -0
  217. package/project/.sdk/tm/zig/test/feature_test.zig +100 -0
  218. package/project/sdkgen-package.json +1 -1
@@ -1,13 +1,12 @@
1
1
  -- ProjectName SDK cache feature
2
2
  --
3
- -- Response caching for safe (read) requests. Wraps the active transport
4
- -- and serves a fresh cached snapshot instead of hitting the network when
5
- -- the same method+URL was fetched within `ttl` ms (default 5000). Only
6
- -- successful (2xx) responses to cacheable methods (default: GET) are
7
- -- stored, keyed by method+URL. The cache is bounded (`max` entries,
8
- -- default 256, oldest evicted first) and every hit/miss/bypass is counted
9
- -- on `client._cache`. Snapshots are replayed with a re-readable `json()`
10
- -- body so both the original caller and later hits can parse repeatedly.
3
+ -- Response caching for safe (read) requests, per client: see the features
4
+ -- reference for the key, the write invalidation and the Cache-Control
5
+ -- rules.
6
+
7
+ -- Lua has no HMAC: SipHash-2-4 under a random per-client key separates
8
+ -- header sets within one store without holding a header value. It is not a
9
+ -- cryptographic commitment.
11
10
 
12
11
  local BaseFeature = require("feature.base_feature")
13
12
  local media = require("utility.media")
@@ -16,6 +15,138 @@ local CacheFeature = {}
16
15
  CacheFeature.__index = CacheFeature
17
16
  setmetatable(CacheFeature, { __index = BaseFeature })
18
17
 
18
+ local UNSTORED = { ["no-store"] = true, ["private"] = true, ["no-cache"] = true }
19
+
20
+
21
+ local function rotl(x, b)
22
+ return (x << b) | (x >> (64 - b))
23
+ end
24
+
25
+
26
+ local function sipround(v0, v1, v2, v3)
27
+ v0 = v0 + v1; v1 = rotl(v1, 13); v1 = v1 ~ v0; v0 = rotl(v0, 32)
28
+ v2 = v2 + v3; v3 = rotl(v3, 16); v3 = v3 ~ v2
29
+ v0 = v0 + v3; v3 = rotl(v3, 21); v3 = v3 ~ v0
30
+ v2 = v2 + v1; v1 = rotl(v1, 17); v1 = v1 ~ v2; v2 = rotl(v2, 32)
31
+ return v0, v1, v2, v3
32
+ end
33
+
34
+
35
+ local function siphash24(key, data)
36
+ local k0, k1 = string.unpack("<i8<i8", key)
37
+ local v0 = 0x736f6d6570736575 ~ k0
38
+ local v1 = 0x646f72616e646f6d ~ k1
39
+ local v2 = 0x6c7967656e657261 ~ k0
40
+ local v3 = 0x7465646279746573 ~ k1
41
+ local len = #data
42
+ local full = len - (len % 8)
43
+ for i = 1, full, 8 do
44
+ local m = string.unpack("<i8", data, i)
45
+ v3 = v3 ~ m
46
+ v0, v1, v2, v3 = sipround(v0, v1, v2, v3)
47
+ v0, v1, v2, v3 = sipround(v0, v1, v2, v3)
48
+ v0 = v0 ~ m
49
+ end
50
+ local b = len << 56
51
+ for i = 0, (len % 8) - 1 do
52
+ b = b | (string.byte(data, full + i + 1) << (8 * i))
53
+ end
54
+ v3 = v3 ~ b
55
+ v0, v1, v2, v3 = sipround(v0, v1, v2, v3)
56
+ v0, v1, v2, v3 = sipround(v0, v1, v2, v3)
57
+ v0 = v0 ~ b
58
+ v2 = v2 ~ 0xff
59
+ for _ = 1, 4 do
60
+ v0, v1, v2, v3 = sipround(v0, v1, v2, v3)
61
+ end
62
+ return v0 ~ v1 ~ v2 ~ v3
63
+ end
64
+
65
+
66
+ -- The system's random source where it has one; otherwise math.random, which
67
+ -- is not a cryptographic generator.
68
+ CacheFeature._siphash24 = siphash24
69
+
70
+
71
+ local function cache_secret()
72
+ local f = io.open("/dev/urandom", "rb")
73
+ local bytes = f and f:read(16) or ""
74
+ if f then
75
+ f:close()
76
+ end
77
+ while #bytes < 16 do
78
+ bytes = bytes .. string.char(math.random(0, 255))
79
+ end
80
+ return bytes
81
+ end
82
+
83
+
84
+ local function header_text(v)
85
+ if v == nil then
86
+ return nil
87
+ end
88
+ if math.type(v) == "float" and v == math.floor(v) then
89
+ return string.format("%d", math.tointeger(v))
90
+ end
91
+ return tostring(v)
92
+ end
93
+
94
+
95
+ -- The configured ttl capped by the response's Cache-Control; zero when the
96
+ -- response may not be stored.
97
+ local function cache_lifetime(headers, ttl)
98
+ local control = ""
99
+ for k, v in pairs(headers or {}) do
100
+ if type(k) == "string" and string.lower(k) == "cache-control" then
101
+ control = string.lower(header_text(v) or "")
102
+ end
103
+ end
104
+ local life = ttl
105
+ for part in string.gmatch(control .. ",", "([^,]*),") do
106
+ local directive = string.match(part, "^%s*(.-)%s*$")
107
+ if UNSTORED[directive] then
108
+ return 0
109
+ end
110
+ local secs = string.match(directive, '^max%-age%s*=%s*"?(%d+)"?$')
111
+ if secs ~= nil then
112
+ life = math.min(life, tonumber(secs) * 1000)
113
+ end
114
+ end
115
+ return life
116
+ end
117
+
118
+
119
+ local function url_scope(url)
120
+ local base = string.match(url, "^[^?#]*")
121
+ return (string.gsub(base, "/+$", ""))
122
+ end
123
+
124
+
125
+ local function url_parent(scope)
126
+ local root = 0
127
+ local scheme = string.find(scope, "://", 1, true)
128
+ if scheme ~= nil then
129
+ root = string.find(scope, "/", scheme + 3, true)
130
+ if root == nil then
131
+ return scope
132
+ end
133
+ root = root - 1
134
+ end
135
+ local cut = 0
136
+ for i = #scope, 1, -1 do
137
+ if string.sub(scope, i, i) == "/" then
138
+ cut = i - 1
139
+ break
140
+ end
141
+ end
142
+ return string.sub(scope, 1, math.max(cut, root))
143
+ end
144
+
145
+
146
+ local function scope_under(scope, prefix)
147
+ return scope == prefix or string.sub(scope, 1, #prefix + 1) == prefix .. "/"
148
+ end
149
+
19
150
 
20
151
  function CacheFeature.new()
21
152
  local self = setmetatable(BaseFeature.new(), CacheFeature)
@@ -46,6 +177,7 @@ function CacheFeature:init(ctx, options)
46
177
 
47
178
  self.store = {}
48
179
  self.order = {}
180
+ self.secret = cache_secret()
49
181
 
50
182
  local cache_self = self
51
183
  local utility = ctx.utility
@@ -72,10 +204,19 @@ function CacheFeature:_through(ctx, fullurl, fetchdef, inner)
72
204
  end
73
205
  end
74
206
  if not cacheable_method then
75
- return inner(ctx, fullurl, fetchdef)
207
+ if method == "GET" or method == "HEAD" then
208
+ return inner(ctx, fullurl, fetchdef)
209
+ end
210
+ local out = table.pack(pcall(inner, ctx, fullurl, fetchdef))
211
+ self:_invalidate(fullurl)
212
+ if not out[1] then
213
+ error(out[2], 0)
214
+ end
215
+ return table.unpack(out, 2, out.n)
76
216
  end
77
217
 
78
- local key = method .. " " .. fullurl
218
+ local headers = fetchdef ~= nil and fetchdef["headers"] or nil
219
+ local key = method .. " " .. fullurl .. " " .. self:_digest(headers)
79
220
  local now = self:_now()
80
221
  local hit = self.store[key]
81
222
 
@@ -94,12 +235,16 @@ function CacheFeature:_through(ctx, fullurl, fetchdef, inner)
94
235
 
95
236
  if self:_cacheable(res, err) then
96
237
  local snapshot = self:_snapshot(res)
97
- local ttl = self.options["ttl"] == nil and 5000 or self.options["ttl"]
98
- self:_evict()
99
- if self.store[key] == nil then
100
- table.insert(self.order, key)
238
+ local ttl = cache_lifetime(snapshot.headers,
239
+ self.options["ttl"] == nil and 5000 or self.options["ttl"])
240
+ if ttl <= 0 then
241
+ self:_track("bypass")
242
+ return self:_replay(snapshot), nil
101
243
  end
102
- self.store[key] = { expiry = now + ttl, snapshot = snapshot }
244
+ self:_drop(key)
245
+ self:_evict()
246
+ table.insert(self.order, key)
247
+ self.store[key] = { expiry = now + ttl, scope = url_scope(fullurl), snapshot = snapshot }
103
248
  self:_track("miss")
104
249
  return self:_replay(snapshot), nil
105
250
  end
@@ -109,6 +254,58 @@ function CacheFeature:_through(ctx, fullurl, fetchdef, inner)
109
254
  end
110
255
 
111
256
 
257
+ function CacheFeature:_digest(headers)
258
+ local pairs_ = {}
259
+ if type(headers) == "table" then
260
+ for k, v in pairs(headers) do
261
+ local text = type(k) == "string" and header_text(v) or nil
262
+ if text ~= nil then
263
+ table.insert(pairs_, { string.lower(k), text })
264
+ end
265
+ end
266
+ end
267
+ table.sort(pairs_, function(a, b)
268
+ if a[1] ~= b[1] then
269
+ return a[1] < b[1]
270
+ end
271
+ return a[2] < b[2]
272
+ end)
273
+ local parts = {}
274
+ for _, p in ipairs(pairs_) do
275
+ table.insert(parts, p[1] .. ":" .. p[2] .. "\n")
276
+ end
277
+ return string.format("%016x", siphash24(self.secret, table.concat(parts)))
278
+ end
279
+
280
+
281
+ function CacheFeature:_invalidate(url)
282
+ local path = url_scope(url)
283
+ local collection = url_parent(path)
284
+ local dropped = {}
285
+ for key, entry in pairs(self.store) do
286
+ if scope_under(entry.scope, path) or entry.scope == collection then
287
+ table.insert(dropped, key)
288
+ end
289
+ end
290
+ for _, key in ipairs(dropped) do
291
+ self:_drop(key)
292
+ end
293
+ end
294
+
295
+
296
+ function CacheFeature:_drop(key)
297
+ if self.store[key] == nil then
298
+ return
299
+ end
300
+ self.store[key] = nil
301
+ for i = #self.order, 1, -1 do
302
+ if self.order[i] == key then
303
+ table.remove(self.order, i)
304
+ end
305
+ end
306
+ end
307
+
308
+
112
309
  function CacheFeature:_cacheable(res, err)
113
310
  -- A body the transport could not read as JSON passes through, for the operation to report.
114
311
  if err ~= nil or res == nil or res["unreadable"] == true then
@@ -68,6 +68,7 @@ function DebugFeature:PreRequest(ctx)
68
68
  local entry = {
69
69
  op = ((ctx.op ~= nil and ctx.op.entity) or "_") .. "." ..
70
70
  ((ctx.op ~= nil and ctx.op.name) or "_"),
71
+ correlationId = ctx.id,
71
72
  method = method,
72
73
  url = url,
73
74
  headers = self:_redact(ctx, headers),
@@ -63,8 +63,8 @@ local json = require("dkjson")
63
63
  local helpers = require("core.helpers")
64
64
  local BaseFeature = require("feature.base_feature")
65
65
  local sekreto = require("feature.secrets.sekreto")
66
- local tls = require("utility.tls")
67
66
  local base64 = require("utility.clean").base64
67
+ local fetcher_util = require("utility.fetcher")
68
68
 
69
69
 
70
70
  local SecretsFeature = {}
@@ -469,6 +469,9 @@ function SecretsFeature:_transport(ctx, fullurl, fetchdef, inner)
469
469
  -- settled success is free, and with `cache = false` the chain is asked
470
470
  -- once per REQUEST, which is that option's meaning.
471
471
  local refused = self:_resolve()
472
+ if type(refused) == "table" and refused.is_sdk_error then
473
+ return nil, refused
474
+ end
472
475
  if refused ~= nil then
473
476
  return nil, ctx:make_error("secrets_refused", sekreto.errmessage(refused))
474
477
  end
@@ -637,77 +640,6 @@ function SecretsFeature:_buy()
637
640
  end
638
641
 
639
642
 
640
- -- The token-exchange transport of last resort: LuaSocket's http, answering
641
- -- the same shape the system.fetch seam promises (status + json). It exists
642
- -- so an exchange works with ordinary SDK options - requiring a custom
643
- -- transport for the COMMON case would reject every live token purchase
644
- -- before a request was made. Mirrors utility/fetcher.lua's own default
645
- -- (which is a local there, not exported); like it, this is HTTP through
646
- -- socket.http, and an https token endpoint needs luasec on the package path,
647
- -- which the rockspec does not require, and is verified as utility/tls says.
648
- local function raw_exchange_fetch(fullurl, fetchdef)
649
- local http_ok, http = pcall(require, "socket.http")
650
- local ltn12_ok, ltn12 = pcall(require, "ltn12")
651
- if not http_ok or not ltn12_ok then
652
- return nil, "HTTP library not available (install luasocket)"
653
- end
654
-
655
- local headers = {}
656
- for k, v in pairs(fetchdef["headers"] or {}) do
657
- if type(v) == "string" then
658
- headers[k] = v
659
- end
660
- end
661
-
662
- local body = fetchdef["body"]
663
- if type(body) ~= "string" then
664
- body = nil
665
- end
666
- if body ~= nil then
667
- headers["content-length"] = tostring(#body)
668
- end
669
-
670
- local create, refused = tls.create(fullurl)
671
- if refused ~= nil then
672
- return nil, refused
673
- end
674
-
675
- local chunks = {}
676
- local res, code, rawheaders = http.request({
677
- url = fullurl,
678
- method = fetchdef["method"] or "POST",
679
- headers = headers,
680
- source = body and ltn12.source.string(body) or nil,
681
- sink = ltn12.sink.table(chunks),
682
- create = create,
683
- redirect = false,
684
- })
685
-
686
- if not res then
687
- return nil, code
688
- end
689
-
690
- local raw = table.concat(chunks)
691
- local resheaders = {}
692
- for k, v in pairs(type(rawheaders) == "table" and rawheaders or {}) do
693
- resheaders[string.lower(k)] = v
694
- end
695
-
696
- return {
697
- status = code,
698
- headers = resheaders,
699
- json = function()
700
- if #raw == 0 then
701
- return nil
702
- end
703
- local decoded = json.decode(raw)
704
- return decoded
705
- end,
706
- body = raw,
707
- }, nil
708
- end
709
-
710
-
711
643
  function SecretsFeature:_buy_once()
712
644
  local x = self._exchange
713
645
 
@@ -728,14 +660,12 @@ function SecretsFeature:_buy_once()
728
660
  base = base:gsub("/+$", "")
729
661
  local url = base .. "/" .. (x.path:gsub("^/+", ""))
730
662
 
731
- -- Deliberately NOT the SDK transport. The transport is what this feature
732
- -- wraps, and sending the token request back through it would recurse on
733
- -- the first expiry - and would route the exchange through the test mock,
734
- -- which knows nothing about it. system.fetch, when the caller supplied
735
- -- one, is the raw seam beneath the transport and is honoured.
736
- local fetch = vs.getpath(options, "system.fetch")
737
- if type(fetch) ~= "function" then
738
- fetch = raw_exchange_fetch
663
+ -- The SDK's own transport, beneath every feature's wrapper: it holds the
664
+ -- test feature's block and the GET-only guard, and the wrapped one would
665
+ -- recurse on the first expiry.
666
+ local rootctx = self._rootctx
667
+ local fetch = function(at, fd)
668
+ return fetcher_util(rootctx, at, fd)
739
669
  end
740
670
 
741
671
  -- The body is ENCODED, never concatenated: a refresh token (or a
@@ -747,6 +677,8 @@ function SecretsFeature:_buy_once()
747
677
  body = json.encode({ [x.request] = self._refresh }),
748
678
  -- Never followed by the client: a redirect would carry the refresh token on.
749
679
  redirect = "manual",
680
+ -- Its own bound, in milliseconds: the timeout feature's wrapper is above it.
681
+ timeout = 30000,
750
682
  }
751
683
  local res, err = fetch(url, fetchdef)
752
684
 
@@ -761,6 +693,9 @@ function SecretsFeature:_buy_once()
761
693
  res, err = fetch(at, fetchdef)
762
694
  end
763
695
 
696
+ if type(err) == "table" and err.is_sdk_error then
697
+ return nil, err
698
+ end
764
699
  if err ~= nil then
765
700
  return nil, sekreto.SekretoError(
766
701
  "secrets: token exchange failed: " .. tostring(err) .. " from " .. url)
@@ -64,6 +64,7 @@ function TelemetryFeature:PrePoint(ctx)
64
64
  local span = {
65
65
  traceId = self:_id("trace"),
66
66
  spanId = self:_id("span"),
67
+ correlationId = ctx.id,
67
68
  name = ((ctx.op ~= nil and ctx.op.entity) or "_") .. "." ..
68
69
  ((ctx.op ~= nil and ctx.op.name) or "_"),
69
70
  start = self:_now(),
@@ -718,6 +718,87 @@ describe("feature", function()
718
718
  h.op({ op = "load", path = "/x" })
719
719
  assert.are.equal(2, #rec.calls)
720
720
  end)
721
+
722
+ -- The reference vectors of the SipHash paper: key 00..0f, message 00..len-1.
723
+ it("matches the SipHash reference vectors", function()
724
+ local CacheFeature = require("feature.cache_feature")
725
+ local bytes = {}
726
+ for i = 0, 63 do bytes[#bytes + 1] = string.char(i) end
727
+ local msg = table.concat(bytes)
728
+ local key = string.sub(msg, 1, 16)
729
+ for _, v in ipairs({
730
+ { 0, "726fdb47dd0e0e31" },
731
+ { 1, "74f839c593dc67fd" },
732
+ { 2, "0d6c8009d9a94f5a" },
733
+ { 3, "85676696d7fb7e2d" },
734
+ { 4, "cf2794e0277187b7" },
735
+ { 5, "18765564cd99a68d" },
736
+ { 6, "cbc9466e58fee3ce" },
737
+ { 7, "ab0200f58b01d137" },
738
+ { 8, "93f5f5799a932462" },
739
+ { 15, "a129ca6149be45e5" },
740
+ { 16, "3f2acc7f57c29bdb" },
741
+ { 63, "958a324ceb064572" },
742
+ }) do
743
+ assert.are.equal(v[2], string.format("%016x", CacheFeature._siphash24(key, string.sub(msg, 1, v[1]))))
744
+ end
745
+ end)
746
+
747
+ it("keys on the request headers", function()
748
+ local rec = recording_server()
749
+ local h = make_client({ features = {
750
+ { name = "cache", options = { ttl = 10000 } },
751
+ }, server = rec.server })
752
+ h.op({ op = "load", path = "/w/1", headers = { ["x-tenant"] = "tenant-a" } })
753
+ h.op({ op = "load", path = "/w/1", headers = { ["X-Tenant"] = "tenant-b" } })
754
+ assert.are.equal(2, #rec.calls)
755
+ h.op({ op = "load", path = "/w/1", headers = { ["x-tenant"] = "tenant-b" } })
756
+ assert.are.equal(2, #rec.calls)
757
+ end)
758
+
759
+ it("drops a write's collection", function()
760
+ local rec = recording_server()
761
+ local h = make_client({ features = {
762
+ { name = "cache", options = { ttl = 10000 } },
763
+ }, server = rec.server })
764
+ local paths = { "/w/1", "/w", "/w/1/part", "/other/1", "/w/2" }
765
+ for _, p in ipairs(paths) do h.op({ op = "load", path = p }) end
766
+ h.op({ op = "update", method = "PUT", path = "/w/1" })
767
+ for _, p in ipairs(paths) do h.op({ op = "load", path = p }) end
768
+ assert.are.equal(9, #rec.calls)
769
+ assert.are.equal(2, h.client._cache.hit)
770
+ end)
771
+
772
+ it("does not store what Cache-Control forbids", function()
773
+ for _, control in ipairs({ "no-store", "private", "No-Cache", "max-age=60, private" }) do
774
+ local rec = recording_server(function()
775
+ return make_response(200, { ok = true }, { ["Cache-Control"] = control })
776
+ end)
777
+ local h = make_client({ features = {
778
+ { name = "cache", options = { ttl = 10000 } },
779
+ }, server = rec.server })
780
+ h.op({ op = "load", path = "/w" })
781
+ h.op({ op = "load", path = "/w" })
782
+ assert.are.equal(2, #rec.calls, control)
783
+ end
784
+ end)
785
+
786
+ it("caps the ttl by max-age", function()
787
+ local clock = make_clock()
788
+ local rec = recording_server(function()
789
+ return make_response(200, { ok = true }, { ["cache-control"] = "max-age=1" })
790
+ end)
791
+ local h = make_client({ features = {
792
+ { name = "cache", options = { ttl = 10000, now = clock.now } },
793
+ }, server = rec.server })
794
+ h.op({ op = "load", path = "/w" })
795
+ clock.advance(500)
796
+ h.op({ op = "load", path = "/w" })
797
+ assert.are.equal(1, #rec.calls)
798
+ clock.advance(600)
799
+ h.op({ op = "load", path = "/w" })
800
+ assert.are.equal(2, #rec.calls)
801
+ end)
721
802
  end) end
722
803
 
723
804
 
@@ -1023,6 +1104,33 @@ describe("feature", function()
1023
1104
  end) end
1024
1105
 
1025
1106
 
1107
+ -- one call, one id ------------------------------------------------------
1108
+ if has_feature("telemetry") and has_feature("debug") and has_feature("audit") then
1109
+ it("telemetry, debug and audit carry one call's context id", function()
1110
+ local h = make_client({ features = {
1111
+ { name = "telemetry" }, { name = "debug" }, { name = "audit" },
1112
+ } })
1113
+ h.op({ op = "load" })
1114
+ h.op({ op = "list" })
1115
+ local function ids(recs)
1116
+ local out = {}
1117
+ for i, rec in ipairs(recs) do
1118
+ out[i] = rec.correlationId
1119
+ end
1120
+ return out
1121
+ end
1122
+ local calls = ids(h.client._audit.records)
1123
+ assert.are.equal(2, #calls)
1124
+ for _, id in ipairs(calls) do
1125
+ assert.is_true(type(id) == "string" and id ~= "")
1126
+ end
1127
+ assert.is_true(calls[1] ~= calls[2])
1128
+ assert.are.same(calls, ids(h.client._telemetry.spans))
1129
+ assert.are.same(calls, ids(h.client._debug.entries))
1130
+ end)
1131
+ end
1132
+
1133
+
1026
1134
  -- clienttrack -------------------------------------------------------------
1027
1135
  if has_feature("clienttrack") then describe("clienttrack", function()
1028
1136
 
@@ -141,16 +141,13 @@ local function make_clean_config(cleanopts)
141
141
  end
142
142
 
143
143
 
144
- -- A context without options (make_error accepts a bare one) still masks by
145
- -- the schema defaults.
144
+ -- A context without a registry (make_error accepts a bare one) still masks
145
+ -- by the schema defaults.
146
146
  local function clean_config(ctx)
147
- if type(ctx) == "table" and type(ctx.options) == "table" then
148
- local derived = ctx.options["__derived__"]
149
- if type(derived) == "table" and type(derived.clean) == "table"
150
- and type(derived.clean.values) == "table"
151
- then
152
- return derived.clean
153
- end
147
+ if type(ctx) == "table" and type(ctx.cleancfg) == "table"
148
+ and type(ctx.cleancfg.values) == "table"
149
+ then
150
+ return ctx.cleancfg
154
151
  end
155
152
 
156
153
  local ok, schema = pcall(require, "schema")
@@ -267,6 +267,14 @@ local function send_once(fullurl, fetchdef)
267
267
  return nil, refused
268
268
  end
269
269
 
270
+ -- A bound in milliseconds, for a caller beneath the timeout feature;
271
+ -- luasocket reads its timeout from the module, so it is restored after.
272
+ local bound = fetchdef["timeout"]
273
+ local was = http.TIMEOUT
274
+ if type(bound) == "number" and bound > 0 then
275
+ http.TIMEOUT = bound / 1000
276
+ end
277
+
270
278
  local response_body = {}
271
279
  local res, code, response_headers = http.request({
272
280
  url = fullurl,
@@ -277,6 +285,7 @@ local function send_once(fullurl, fetchdef)
277
285
  redirect = false,
278
286
  create = create,
279
287
  })
288
+ http.TIMEOUT = was
280
289
 
281
290
  if not res then
282
291
  return nil, code
@@ -180,7 +180,7 @@ local function make_options_util(ctx)
180
180
  vs.clone(cfgclean),
181
181
  vs.clone(rawclean),
182
182
  }))
183
- local cleanctx = { options = { __derived__ = { clean = cleancfg } } }
183
+ local cleanctx = { cleancfg = cleancfg }
184
184
  cleanmod.clean_add_sensitive(cleanctx, secret_scan(opts, { clean = true, extend = true }))
185
185
  for _, block in ipairs({ cfgclean, rawclean }) do
186
186
  for _, raw in ipairs(cleanmod.splitvalues(block.values)) do
@@ -405,10 +405,10 @@ local function make_options_util(ctx)
405
405
  cleanmod.clean_add_sensitive(cleanctx,
406
406
  secret_scan(opts, { clean = true, extend = true, __derived__ = true }))
407
407
 
408
- opts["__derived__"] = {
409
- clean = cleancfg,
410
- featureorder = featureorder,
411
- }
408
+ opts["__derived__"] = { featureorder = featureorder }
409
+
410
+ -- Beside the options, not in them: options_map hands them to callers.
411
+ ctx.cleancfg = cleancfg
412
412
 
413
413
  return opts
414
414
  end
@@ -227,6 +227,9 @@ let make ?(plugins : Defs.definition list = [])
227
227
  * where it places a credential, and Basic only where it composes one. *)
228
228
  let declared = ref false in
229
229
  let basic = ref false in
230
+ (* The transport's refusal of the last purchase, which the gate answers in
231
+ * place of a resolution error. *)
232
+ let refused : sdk_error option ref = ref None in
230
233
 
231
234
  let refuse (msg : string) =
232
235
  (match !initerr with None -> initerr := Some msg | Some _ -> ()) in
@@ -310,17 +313,25 @@ let make ?(plugins : Defs.definition list = [])
310
313
  ("body", Str (jsonify (jo [(x.xrequest, Str !refresh)])));
311
314
  (* Never followed by the client: a redirect would carry the refresh token on. *)
312
315
  ("redirect", Str "manual")] in
316
+ let sysfetch = getpath_s opts "system.fetch" in
313
317
  let fetch =
314
- match getpath_s opts "system.fetch" with
318
+ match sysfetch with
315
319
  | Func _ as sf -> Some (fun url fd -> call_vfn sf (ja [Str url; fd]))
316
320
  | _ -> transport in
317
- match fetch with
318
- | None ->
321
+ (* Not fetcher_util, as every other target does: the core bundles no
322
+ * client, so only its GET-only guard precedes the vendored one. *)
323
+ refused := (match sysfetch, !client with
324
+ | Func _, _ -> None
325
+ | _, Some { cl_rootctx = Some rctx; _ } -> Sdk_runtime.getonly_refusal rctx url fetchdef
326
+ | _ -> None);
327
+ match !refused, fetch with
328
+ | Some e, _ -> Error e.err_msg
329
+ | None, None ->
319
330
  Error ("secrets: the token exchange has no HTTP transport: this SDK's core bundles "
320
331
  ^ "none and no secrets plugin group needing one is active, so the vendored "
321
332
  ^ "HTTP client is not compiled; supply options.system.fetch or activate a "
322
333
  ^ "plugin group")
323
- | Some fetch ->
334
+ | None, Some fetch ->
324
335
  (* By option alone, and only while the exchange stays on its own origin. *)
325
336
  let rec ask at hops =
326
337
  let res = fetch at fetchdef in
@@ -426,7 +437,7 @@ let make ?(plugins : Defs.definition list = [])
426
437
  let current = !cred in
427
438
  let token =
428
439
  if "" <> current && current <> used then Some current
429
- else (match buy x with Ok t -> Some t | Error _ -> None) in
440
+ else (match buy x with Ok t -> Some t | Error _ -> refused := None; None) in
430
441
  match token with
431
442
  (* The purchase failed: answer with the API's own refusal rather
432
443
  * than this one - the refusal is the more useful of the two. *)
@@ -451,7 +462,11 @@ let make ?(plugins : Defs.definition list = [])
451
462
  (match resolve () with
452
463
  | Error msg ->
453
464
  bump_num (bucket ()) "refusals" 1.;
454
- (Noval, Some (ctx_make_error fctx "secrets_provider" msg))
465
+ let blocked = !refused in
466
+ refused := None;
467
+ (Noval, Some (match blocked with
468
+ | Some e -> e
469
+ | None -> ctx_make_error fctx "secrets_provider" msg))
455
470
  | Ok () ->
456
471
  if "" <> !cred then reauth fetchdef !cred;
457
472
  (match !exchange with