@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.
- package/bin/voxgig-sdkgen +1 -1
- package/package.json +1 -1
- package/project/.sdk/model/feature/cache.aontu +6 -4
- package/project/.sdk/model/target/lua.aontu +6 -0
- package/project/.sdk/src/cmp/c/Config_c.ts +3 -134
- package/project/.sdk/src/cmp/c/TestClean_c.ts +14 -0
- package/project/.sdk/src/cmp/clojure/TestClean_clojure.ts +10 -5
- package/project/.sdk/src/cmp/cpp/TestClean_cpp.ts +13 -0
- package/project/.sdk/src/cmp/csharp/TestClean_csharp.ts +9 -0
- package/project/.sdk/src/cmp/elixir/TestClean_elixir.ts +11 -3
- package/project/.sdk/src/cmp/go/TestClean_go.ts +39 -3
- package/project/.sdk/src/cmp/java/TestClean_java.ts +8 -0
- package/project/.sdk/src/cmp/js/TestClean_js.ts +13 -5
- package/project/.sdk/src/cmp/kotlin/TestClean_kotlin.ts +22 -0
- package/project/.sdk/src/cmp/lua/TestClean_lua.ts +12 -3
- package/project/.sdk/src/cmp/ocaml/TestClean_ocaml.ts +18 -4
- package/project/.sdk/src/cmp/perl/TestClean_perl.ts +12 -3
- package/project/.sdk/src/cmp/php/TestClean_php.ts +10 -3
- package/project/.sdk/src/cmp/py/TestClean_py.ts +10 -3
- package/project/.sdk/src/cmp/rb/TestClean_rb.ts +10 -3
- package/project/.sdk/src/cmp/rust/TestClean_rust.ts +10 -0
- package/project/.sdk/src/cmp/scala/TestClean_scala.ts +34 -0
- package/project/.sdk/src/cmp/swift/TestClean_swift.ts +20 -10
- package/project/.sdk/src/cmp/ts/TestClean_ts.ts +13 -5
- package/project/.sdk/src/cmp/zig/TestClean_zig.ts +12 -0
- package/project/.sdk/tm/c/core/context.c +1 -0
- package/project/.sdk/tm/c/core/sdk.h +7 -4
- package/project/.sdk/tm/c/feature/cache.c +243 -13
- package/project/.sdk/tm/c/feature/debug.c +1 -0
- package/project/.sdk/tm/c/feature/secrets.c +27 -38
- package/project/.sdk/tm/c/feature/telemetry.c +1 -0
- package/project/.sdk/tm/c/tests/feature_test.c +142 -0
- package/project/.sdk/tm/c/utility/clean.c +26 -33
- package/project/.sdk/tm/c/utility/make_options.c +9 -8
- package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +14 -11
- package/project/.sdk/tm/clojure/src/sdk/client.clj +3 -1
- package/project/.sdk/tm/clojure/src/sdk/core.clj +19 -11
- package/project/.sdk/tm/clojure/src/sdk/features.clj +83 -10
- package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +47 -0
- package/project/.sdk/tm/clojure/test/sdk/test/pipeline.clj +1 -0
- package/project/.sdk/tm/cpp/Makefile +5 -0
- package/project/.sdk/tm/cpp/core/types.hpp +11 -4
- package/project/.sdk/tm/cpp/feature/audit.hpp +6 -7
- package/project/.sdk/tm/cpp/feature/cache.hpp +188 -9
- package/project/.sdk/tm/cpp/feature/debug.hpp +1 -0
- package/project/.sdk/tm/cpp/feature/secrets.hpp +12 -0
- package/project/.sdk/tm/cpp/feature/telemetry.hpp +1 -0
- package/project/.sdk/tm/cpp/test/feature_test.cpp +128 -0
- package/project/.sdk/tm/cpp/utility/pipeline.hpp +9 -7
- package/project/.sdk/tm/csharp/core/Context.cs +4 -0
- package/project/.sdk/tm/csharp/feature/CacheFeature.cs +126 -12
- package/project/.sdk/tm/csharp/feature/DebugFeature.cs +1 -0
- package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +9 -27
- package/project/.sdk/tm/csharp/feature/TelemetryFeature.cs +1 -0
- package/project/.sdk/tm/csharp/test/FeatureTest.cs +101 -0
- package/project/.sdk/tm/csharp/utility/Clean.cs +5 -10
- package/project/.sdk/tm/csharp/utility/Fetcher.cs +7 -1
- package/project/.sdk/tm/csharp/utility/MakeOptions.cs +5 -12
- package/project/.sdk/tm/elixir/lib/projectname/context.ex +2 -0
- package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +146 -30
- package/project/.sdk/tm/elixir/lib/projectname/feature/debug.ex +1 -0
- package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +10 -56
- package/project/.sdk/tm/elixir/lib/projectname/feature/telemetry.ex +1 -0
- package/project/.sdk/tm/elixir/lib/projectname/utility.ex +18 -12
- package/project/.sdk/tm/elixir/test/feature_test.exs +84 -0
- package/project/.sdk/tm/go/core/context.go +6 -0
- package/project/.sdk/tm/go/feature/cache_feature.go +131 -4
- package/project/.sdk/tm/go/feature/debug_feature.go +3 -2
- package/project/.sdk/tm/go/feature/secrets_feature.go +17 -71
- package/project/.sdk/tm/go/feature/telemetry_feature.go +5 -4
- package/project/.sdk/tm/go/test/feature_corpus_test.go +150 -2
- package/project/.sdk/tm/go/test/feature_test.go +101 -0
- package/project/.sdk/tm/go/utility/clean.go +5 -7
- package/project/.sdk/tm/go/utility/fetcher.go +11 -0
- package/project/.sdk/tm/go/utility/make_options.go +5 -7
- package/project/.sdk/tm/java/core/Context.java +9 -0
- package/project/.sdk/tm/java/feature/CacheFeature.java +123 -17
- package/project/.sdk/tm/java/feature/DebugFeature.java +1 -0
- package/project/.sdk/tm/java/feature/SecretsFeature.java +9 -92
- package/project/.sdk/tm/java/feature/TelemetryFeature.java +1 -0
- package/project/.sdk/tm/java/test/FeatureCorpusTest.java +141 -2
- package/project/.sdk/tm/java/test/FeatureTest.java +88 -0
- package/project/.sdk/tm/java/utility/Clean.java +6 -10
- package/project/.sdk/tm/java/utility/Fetcher.java +5 -1
- package/project/.sdk/tm/java/utility/MakeOptions.java +3 -1
- package/project/.sdk/tm/js/src/Context.js +4 -0
- package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +118 -11
- package/project/.sdk/tm/js/src/feature/cost/CostFeature.js +3 -2
- package/project/.sdk/tm/js/src/feature/debug/DebugFeature.js +1 -0
- package/project/.sdk/tm/js/src/feature/metrics/MetricsFeature.js +1 -1
- package/project/.sdk/tm/js/src/feature/paging/PagingFeature.js +4 -4
- package/project/.sdk/tm/js/src/feature/rbac/RbacFeature.js +4 -4
- package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +16 -6
- package/project/.sdk/tm/js/src/feature/telemetry/TelemetryFeature.js +1 -0
- package/project/.sdk/tm/js/src/feature/validate/ValidateFeature.js +2 -1
- package/project/.sdk/tm/js/src/utility/CleanUtility.js +1 -1
- package/project/.sdk/tm/js/src/utility/MakeOptionsUtility.js +6 -6
- package/project/.sdk/tm/js/test/feature/Corpus.test.js +100 -2
- package/project/.sdk/tm/js/test/feature/harness.js +3 -3
- package/project/.sdk/tm/js/test/feature.test.js +17 -0
- package/project/.sdk/tm/js/test/pipeline.test.js +1 -1
- package/project/.sdk/tm/kotlin/core/Context.kt +3 -0
- package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +102 -7
- package/project/.sdk/tm/kotlin/feature/DebugFeature.kt +1 -0
- package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +10 -20
- package/project/.sdk/tm/kotlin/feature/TelemetryFeature.kt +1 -0
- package/project/.sdk/tm/kotlin/test/FeatureTest.kt +79 -0
- package/project/.sdk/tm/kotlin/utility/CoreUtil.kt +5 -5
- package/project/.sdk/tm/kotlin/utility/Fetcher.kt +4 -0
- package/project/.sdk/tm/kotlin/utility/MakeOptions.kt +3 -1
- package/project/.sdk/tm/lua/core/context.lua +6 -0
- package/project/.sdk/tm/lua/feature/cache_feature.lua +212 -15
- package/project/.sdk/tm/lua/feature/debug_feature.lua +1 -0
- package/project/.sdk/tm/lua/feature/secrets_feature.lua +15 -80
- package/project/.sdk/tm/lua/feature/telemetry_feature.lua +1 -0
- package/project/.sdk/tm/lua/test/feature_test.lua +108 -0
- package/project/.sdk/tm/lua/utility/clean.lua +6 -9
- package/project/.sdk/tm/lua/utility/fetcher.lua +9 -0
- package/project/.sdk/tm/lua/utility/make_options.lua +5 -5
- package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +21 -6
- package/project/.sdk/tm/ocaml/sdk_features.ml +108 -8
- package/project/.sdk/tm/ocaml/sdk_runtime.ml +13 -10
- package/project/.sdk/tm/ocaml/sdk_types.ml +2 -0
- package/project/.sdk/tm/ocaml/test/t_feature.ml +51 -1
- package/project/.sdk/tm/perl/core/context.pm +3 -0
- package/project/.sdk/tm/perl/feature/audit_feature.pm +4 -7
- package/project/.sdk/tm/perl/feature/cache_feature.pm +111 -15
- package/project/.sdk/tm/perl/feature/debug_feature.pm +1 -0
- package/project/.sdk/tm/perl/feature/secrets_feature.pm +13 -18
- package/project/.sdk/tm/perl/feature/telemetry_feature.pm +1 -0
- package/project/.sdk/tm/perl/t/feature.t +86 -0
- package/project/.sdk/tm/perl/t/feature_corpus.t +80 -4
- package/project/.sdk/tm/perl/utility/clean.pm +6 -8
- package/project/.sdk/tm/perl/utility/fetcher.pm +4 -0
- package/project/.sdk/tm/perl/utility/make_options.pm +5 -5
- package/project/.sdk/tm/php/core/Context.php +4 -0
- package/project/.sdk/tm/php/feature/CacheFeature.php +98 -13
- package/project/.sdk/tm/php/feature/DebugFeature.php +1 -0
- package/project/.sdk/tm/php/feature/SecretsFeature.php +18 -15
- package/project/.sdk/tm/php/feature/TelemetryFeature.php +1 -0
- package/project/.sdk/tm/php/test/FeatureCorpusTest.php +113 -3
- package/project/.sdk/tm/php/test/FeatureTest.php +90 -0
- package/project/.sdk/tm/php/utility/Clean.php +3 -10
- package/project/.sdk/tm/php/utility/Fetcher.php +8 -3
- package/project/.sdk/tm/php/utility/MakeOptions.php +5 -7
- package/project/.sdk/tm/py/pkg/core/context.py +4 -0
- package/project/.sdk/tm/py/pkg/feature/cache_feature.py +79 -13
- package/project/.sdk/tm/py/pkg/feature/debug_feature.py +1 -0
- package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +15 -66
- package/project/.sdk/tm/py/pkg/feature/telemetry_feature.py +1 -0
- package/project/.sdk/tm/py/pkg/utility/clean.py +4 -10
- package/project/.sdk/tm/py/pkg/utility/make_options.py +8 -9
- package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +23 -9
- package/project/.sdk/tm/py/test/test_feature.py +73 -0
- package/project/.sdk/tm/py/test/test_feature_corpus.py +88 -3
- package/project/.sdk/tm/rb/core/context.rb +4 -1
- package/project/.sdk/tm/rb/feature/audit_feature.rb +6 -5
- package/project/.sdk/tm/rb/feature/cache_feature.rb +75 -11
- package/project/.sdk/tm/rb/feature/debug_feature.rb +1 -0
- package/project/.sdk/tm/rb/feature/secrets_feature.rb +9 -64
- package/project/.sdk/tm/rb/feature/telemetry_feature.rb +1 -0
- package/project/.sdk/tm/rb/test/feature_corpus_test.rb +85 -3
- package/project/.sdk/tm/rb/test/feature_test.rb +93 -0
- package/project/.sdk/tm/rb/utility/clean.rb +6 -7
- package/project/.sdk/tm/rb/utility/fetcher.rb +6 -0
- package/project/.sdk/tm/rb/utility/make_options.rb +5 -5
- package/project/.sdk/tm/rust/core/context.rs +8 -0
- package/project/.sdk/tm/rust/feature/cache.rs +120 -3
- package/project/.sdk/tm/rust/feature/debug.rs +1 -0
- package/project/.sdk/tm/rust/feature/secrets.rs +36 -98
- package/project/.sdk/tm/rust/feature/telemetry.rs +1 -0
- package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +3 -3
- package/project/.sdk/tm/rust/tests/feature_test.rs +141 -0
- package/project/.sdk/tm/rust/utility/clean.rs +35 -43
- package/project/.sdk/tm/rust/utility/fetcher.rs +6 -0
- package/project/.sdk/tm/rust/utility/make_options.rs +8 -11
- package/project/.sdk/tm/scala/core/Context.scala +4 -0
- package/project/.sdk/tm/scala/feature/CacheFeature.scala +117 -11
- package/project/.sdk/tm/scala/feature/DebugFeature.scala +1 -0
- package/project/.sdk/tm/scala/feature/SecretsFeature.scala +11 -24
- package/project/.sdk/tm/scala/feature/TelemetryFeature.scala +1 -0
- package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +52 -1
- package/project/.sdk/tm/scala/utility/Fetcher.scala +5 -0
- package/project/.sdk/tm/scala/utility/Make.scala +3 -1
- package/project/.sdk/tm/scala/utility/Misc.scala +5 -8
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Context.swift +3 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +122 -11
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/DebugFeature.swift +1 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +9 -13
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/TelemetryFeature.swift +1 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Fetcher.swift +4 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/MakeOptions.swift +5 -4
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/ResultUtil.swift +4 -5
- package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +79 -0
- package/project/.sdk/tm/ts/src/Context.ts +4 -0
- package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +115 -4
- package/project/.sdk/tm/ts/src/feature/cost/CostFeature.ts +3 -2
- package/project/.sdk/tm/ts/src/feature/debug/DebugFeature.ts +1 -0
- package/project/.sdk/tm/ts/src/feature/metrics/MetricsFeature.ts +1 -1
- package/project/.sdk/tm/ts/src/feature/paging/PagingFeature.ts +4 -4
- package/project/.sdk/tm/ts/src/feature/rbac/RbacFeature.ts +4 -4
- package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +14 -2
- package/project/.sdk/tm/ts/src/feature/telemetry/TelemetryFeature.ts +1 -0
- package/project/.sdk/tm/ts/src/feature/validate/ValidateFeature.ts +2 -1
- package/project/.sdk/tm/ts/src/utility/CleanUtility.ts +1 -1
- package/project/.sdk/tm/ts/src/utility/MakeOptionsUtility.ts +6 -6
- package/project/.sdk/tm/ts/test/feature/Corpus.test.ts +101 -2
- package/project/.sdk/tm/ts/test/feature/harness.ts +3 -3
- package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
- package/project/.sdk/tm/ts/test/pipeline.test.ts +1 -1
- package/project/.sdk/tm/zig/core/context.zig +5 -0
- package/project/.sdk/tm/zig/core/utility.zig +11 -9
- package/project/.sdk/tm/zig/feature/cache.zig +144 -13
- package/project/.sdk/tm/zig/feature/debug.zig +1 -0
- package/project/.sdk/tm/zig/feature/secrets.zig +6 -1
- package/project/.sdk/tm/zig/feature/telemetry.zig +1 -0
- package/project/.sdk/tm/zig/test/feature_test.zig +100 -0
- package/project/sdkgen-package.json +1 -1
|
@@ -18,6 +18,8 @@ class Context(ctxmap: JMap[String, Object], basectx: Context) {
|
|
|
18
18
|
var config: JMap[String, Object] = null
|
|
19
19
|
var entopts: JMap[String, Object] = null
|
|
20
20
|
var options: JMap[String, Object] = null
|
|
21
|
+
// The clean registry, beside the options rather than in them.
|
|
22
|
+
var cleancfg: Object = null
|
|
21
23
|
var opmap: JMap[String, Operation] = null
|
|
22
24
|
var response: Response = null
|
|
23
25
|
var result: Result = null
|
|
@@ -73,6 +75,8 @@ class Context(ctxmap: JMap[String, Object], basectx: Context) {
|
|
|
73
75
|
// Options
|
|
74
76
|
Helpers.getCtxProp(ctxmap, "options") match { case o: JMap[_, _] => options = o.asInstanceOf[JMap[String, Object]]; case _ => }
|
|
75
77
|
if (options == null && basectx != null) options = basectx.options
|
|
78
|
+
cleancfg = Helpers.getCtxProp(ctxmap, "cleancfg")
|
|
79
|
+
if (cleancfg == null && basectx != null) cleancfg = basectx.cleancfg
|
|
76
80
|
|
|
77
81
|
// Entity
|
|
78
82
|
Helpers.getCtxProp(ctxmap, "entity") match { case e: Entity => entity = e; case _ => }
|
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
package SCALAPACKAGE.feature
|
|
2
2
|
|
|
3
|
+
import java.nio.charset.StandardCharsets
|
|
4
|
+
import java.security.SecureRandom
|
|
3
5
|
import java.util.{ArrayList, LinkedHashMap, List => JList, Map => JMap}
|
|
4
6
|
import java.util.function.Supplier
|
|
7
|
+
import javax.crypto.Mac
|
|
8
|
+
import javax.crypto.spec.SecretKeySpec
|
|
5
9
|
import SCALAPACKAGE.core.{Context, FetcherFn, SdkClient}
|
|
6
10
|
import SCALAPACKAGE.utility.Media
|
|
7
11
|
|
|
8
|
-
// Response caching for safe (read) requests
|
|
9
|
-
//
|
|
10
|
-
// same method+URL was fetched within `ttl` ms (default: 5000). Only
|
|
11
|
-
// successful (2xx) responses to cacheable methods (default: GET) are
|
|
12
|
-
// stored, keyed by method+URL. The cache is bounded (`max` entries, default
|
|
13
|
-
// 256, oldest evicted first) and every hit/miss/bypass is counted. Bodies
|
|
14
|
-
// are snapshotted on capture so both the current caller and later hits can
|
|
15
|
-
// re-read the JSON body repeatedly.
|
|
12
|
+
// Response caching for safe (read) requests, per client: see the features
|
|
13
|
+
// reference for the key, the write invalidation and the Cache-Control rules.
|
|
16
14
|
class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
|
|
17
15
|
|
|
18
16
|
private var client: SdkClient = null
|
|
19
17
|
private var options: JMap[String, Object] = null
|
|
20
18
|
private var store: JMap[String, CacheEntry] = null
|
|
21
19
|
private var order: JList[String] = null
|
|
20
|
+
private var secret: SecretKeySpec = null
|
|
22
21
|
|
|
23
22
|
// Activity tracking (mirrors the ts client._cache record).
|
|
24
23
|
var hit: Int = 0
|
|
@@ -27,6 +26,7 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
|
|
|
27
26
|
|
|
28
27
|
private class CacheEntry {
|
|
29
28
|
var expiry: Long = 0
|
|
29
|
+
var scope: String = ""
|
|
30
30
|
var snapshot: CacheSnapshot = null
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -48,6 +48,9 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
|
|
|
48
48
|
|
|
49
49
|
this.store = new LinkedHashMap[String, CacheEntry]()
|
|
50
50
|
this.order = new ArrayList[String]()
|
|
51
|
+
val bytes = new Array[Byte](32)
|
|
52
|
+
new SecureRandom().nextBytes(bytes)
|
|
53
|
+
this.secret = new SecretKeySpec(bytes, "HmacSHA256")
|
|
51
54
|
|
|
52
55
|
val inner: FetcherFn = ctx.utility.fetcher
|
|
53
56
|
|
|
@@ -77,10 +80,17 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
|
|
|
77
80
|
}
|
|
78
81
|
}
|
|
79
82
|
if (!cacheable) {
|
|
80
|
-
|
|
83
|
+
if (CacheFeature.SafeMethods.contains(method)) {
|
|
84
|
+
return inner(ctx, url, fetchdef)
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
return inner(ctx, url, fetchdef)
|
|
88
|
+
} finally {
|
|
89
|
+
invalidate(url)
|
|
90
|
+
}
|
|
81
91
|
}
|
|
82
92
|
|
|
83
|
-
val key = method + " " + url
|
|
93
|
+
val key = method + " " + url + " " + digest(fetchdef.get("headers"))
|
|
84
94
|
val now = FeatureOptions.foptNow(this.options).getAsLong()
|
|
85
95
|
|
|
86
96
|
val hitEntry = this.store.get(key)
|
|
@@ -106,10 +116,16 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
|
|
|
106
116
|
|
|
107
117
|
if (storable(res)) {
|
|
108
118
|
val snapshot = this.snapshot(res)
|
|
109
|
-
val ttl = FeatureOptions.foptInt(this.options, "ttl", 5000)
|
|
119
|
+
val ttl = CacheFeature.lifetime(snapshot.headers, FeatureOptions.foptInt(this.options, "ttl", 5000).toLong)
|
|
120
|
+
if (ttl <= 0) {
|
|
121
|
+
this.bypass += 1
|
|
122
|
+
return replay(snapshot)
|
|
123
|
+
}
|
|
124
|
+
drop(key)
|
|
110
125
|
evict()
|
|
111
126
|
val entry = new CacheEntry()
|
|
112
127
|
entry.expiry = now + ttl
|
|
128
|
+
entry.scope = CacheFeature.scope(url)
|
|
113
129
|
entry.snapshot = snapshot
|
|
114
130
|
this.store.put(key, entry)
|
|
115
131
|
this.order.add(key)
|
|
@@ -121,6 +137,48 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
|
|
|
121
137
|
res
|
|
122
138
|
}
|
|
123
139
|
|
|
140
|
+
private def digest(headers: Object): String = {
|
|
141
|
+
val pairs = new ArrayList[(String, String)]()
|
|
142
|
+
headers match {
|
|
143
|
+
case hm: JMap[_, _] =>
|
|
144
|
+
val it = hm.entrySet().iterator()
|
|
145
|
+
while (it.hasNext) {
|
|
146
|
+
val h = it.next()
|
|
147
|
+
if (h.getValue != null) {
|
|
148
|
+
pairs.add((String.valueOf(h.getKey).toLowerCase(), String.valueOf(h.getValue)))
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
case _ =>
|
|
152
|
+
}
|
|
153
|
+
val sorted = pairs.toArray(new Array[(String, String)](0)).sortBy(identity)
|
|
154
|
+
val mac = Mac.getInstance("HmacSHA256")
|
|
155
|
+
mac.init(this.secret)
|
|
156
|
+
for ((name, value) <- sorted) {
|
|
157
|
+
mac.update((name + ":" + value + "\n").getBytes(StandardCharsets.UTF_8))
|
|
158
|
+
}
|
|
159
|
+
mac.doFinal().map(b => f"$b%02x").mkString
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private def invalidate(url: String): Unit = {
|
|
163
|
+
val path = CacheFeature.scope(url)
|
|
164
|
+
val collection = CacheFeature.parent(path)
|
|
165
|
+
val dropped = new ArrayList[String]()
|
|
166
|
+
val it = this.store.entrySet().iterator()
|
|
167
|
+
while (it.hasNext) {
|
|
168
|
+
val e = it.next()
|
|
169
|
+
if (CacheFeature.under(e.getValue.scope, path) || e.getValue.scope == collection) {
|
|
170
|
+
dropped.add(e.getKey)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
dropped.forEach(k => drop(k))
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private def drop(key: String): Unit = {
|
|
177
|
+
if (this.store.remove(key) != null) {
|
|
178
|
+
this.order.remove(key)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
124
182
|
// A body the transport could not read as JSON passes through, for the operation to report.
|
|
125
183
|
private def storable(res: Object): Boolean = {
|
|
126
184
|
val status = FeatureOptions.fresStatus(res)
|
|
@@ -193,3 +251,51 @@ class CacheFeature extends BaseFeature("cache", "0.0.1", true) {
|
|
|
193
251
|
}
|
|
194
252
|
}
|
|
195
253
|
}
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
object CacheFeature {
|
|
257
|
+
private val SafeMethods = Set("GET", "HEAD")
|
|
258
|
+
private val Unstored = Set("no-store", "private", "no-cache")
|
|
259
|
+
private val MaxAge = "^max-age\\s*=\\s*\"?(\\d+)\"?$".r
|
|
260
|
+
|
|
261
|
+
// The configured ttl capped by the response's Cache-Control; zero when the
|
|
262
|
+
// response may not be stored.
|
|
263
|
+
private def lifetime(headers: JMap[String, Object], ttl: Long): Long = {
|
|
264
|
+
var control = ""
|
|
265
|
+
val it = headers.entrySet().iterator()
|
|
266
|
+
while (it.hasNext) {
|
|
267
|
+
val h = it.next()
|
|
268
|
+
if (h.getKey.toLowerCase() == "cache-control") {
|
|
269
|
+
control = String.valueOf(h.getValue).toLowerCase()
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
val directives = control.split(",").map(_.trim)
|
|
273
|
+
if (directives.exists(Unstored.contains)) {
|
|
274
|
+
0L
|
|
275
|
+
} else {
|
|
276
|
+
directives.foldLeft(ttl) { (life, directive) =>
|
|
277
|
+
directive match {
|
|
278
|
+
case MaxAge(secs) => secs.toLongOption.fold(life)(n => math.min(life, n * 1000))
|
|
279
|
+
case _ => life
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private def scope(url: String): String = {
|
|
286
|
+
var out = url.split("[?#]", 2)(0)
|
|
287
|
+
while (out.endsWith("/")) {
|
|
288
|
+
out = out.substring(0, out.length - 1)
|
|
289
|
+
}
|
|
290
|
+
out
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
private def parent(scope: String): String = {
|
|
294
|
+
val scheme = scope.indexOf("://")
|
|
295
|
+
val root = if (scheme < 0) 0 else scope.indexOf('/', scheme + 3)
|
|
296
|
+
if (root < 0) scope else scope.substring(0, math.max(scope.lastIndexOf('/'), root))
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private def under(scope: String, prefix: String): Boolean =
|
|
300
|
+
scope == prefix || scope.startsWith(prefix + "/")
|
|
301
|
+
}
|
|
@@ -39,6 +39,7 @@ class DebugFeature extends BaseFeature("debug", "0.0.1", true) {
|
|
|
39
39
|
|
|
40
40
|
val entry = new LinkedHashMap[String, Object]()
|
|
41
41
|
entry.put("op", entity + "." + opname)
|
|
42
|
+
entry.put("correlationId", ctx.id)
|
|
42
43
|
entry.put("start", java.lang.Long.valueOf(FeatureOptions.foptNow(this.options).getAsLong()))
|
|
43
44
|
if (ctx.spec != null) {
|
|
44
45
|
entry.put("method", ctx.spec.method)
|
|
@@ -65,6 +65,7 @@ import voxgig.plugin.{Definition, VBool, VList, VMap, VNull, VNum, VStr, Value}
|
|
|
65
65
|
class SecretsFeature extends BaseFeature("secrets", "0.1.0", true) {
|
|
66
66
|
|
|
67
67
|
private var client: SdkClient = null
|
|
68
|
+
private var rootctx: Context = null
|
|
68
69
|
|
|
69
70
|
// Every value this feature resolves or buys is a secret the SDK handles,
|
|
70
71
|
// and none arrives under an option key the intake registration saw.
|
|
@@ -141,6 +142,7 @@ class SecretsFeature extends BaseFeature("secrets", "0.1.0", true) {
|
|
|
141
142
|
// Sync by feature contract: build the chain, never look anything up here.
|
|
142
143
|
override def init(ctx: Context, options: JMap[String, Object]): Unit = {
|
|
143
144
|
this.client = ctx.client
|
|
145
|
+
this.rootctx = ctx
|
|
144
146
|
this.liveopts = ctx.options
|
|
145
147
|
this.active = FeatureOptions.foptBool(options, "active", false)
|
|
146
148
|
|
|
@@ -517,6 +519,8 @@ class SecretsFeature extends BaseFeature("secrets", "0.1.0", true) {
|
|
|
517
519
|
fetchdef.put("body", Struct.jsonify(bodymap, flags))
|
|
518
520
|
// Never followed by the client: a redirect would carry the refresh token on.
|
|
519
521
|
fetchdef.put("redirect", "manual")
|
|
522
|
+
// Its own bound, in milliseconds: the timeout feature's wrapper is above it.
|
|
523
|
+
fetchdef.put("timeout", java.lang.Integer.valueOf(30000))
|
|
520
524
|
|
|
521
525
|
var res = exchangefetch(url, fetchdef)
|
|
522
526
|
|
|
@@ -568,34 +572,17 @@ class SecretsFeature extends BaseFeature("secrets", "0.1.0", true) {
|
|
|
568
572
|
token
|
|
569
573
|
}
|
|
570
574
|
|
|
571
|
-
//
|
|
572
|
-
//
|
|
573
|
-
// the first expiry
|
|
574
|
-
// which knows nothing about it. `system.fetch` when the caller supplied
|
|
575
|
-
// one, else the platform fetcher directly: Fetcher.defaultHttpFetch
|
|
576
|
-
// already answers in the {status, headers, json} shape the seam promises
|
|
577
|
-
// and, unlike Fetcher.fetcher, is gated on neither mode nor the test
|
|
578
|
-
// feature.
|
|
575
|
+
// The SDK's own transport, beneath every feature's wrapper: it holds the
|
|
576
|
+
// test feature's block and the GET-only guard, and the wrapped one would
|
|
577
|
+
// recurse on the first expiry.
|
|
579
578
|
private def exchangefetch(
|
|
580
579
|
url: String,
|
|
581
580
|
fetchdef: JMap[String, Object],
|
|
582
|
-
): JMap[String, Object] =
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
if (sysfetch eq Struct.UNDEF) sysfetch = null
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
sysfetch match {
|
|
590
|
-
case bf: BiFunction[_, _, _] =>
|
|
591
|
-
bf.asInstanceOf[BiFunction[String, JMap[String, Object], Object]]
|
|
592
|
-
.apply(url, fetchdef) match {
|
|
593
|
-
case m: JMap[_, _] => m.asInstanceOf[JMap[String, Object]]
|
|
594
|
-
case _ => null
|
|
595
|
-
}
|
|
596
|
-
case _ => Fetcher.defaultHttpFetch(url, fetchdef)
|
|
581
|
+
): JMap[String, Object] =
|
|
582
|
+
Fetcher.fetcher(this.rootctx, url, fetchdef) match {
|
|
583
|
+
case m: JMap[_, _] => m.asInstanceOf[JMap[String, Object]]
|
|
584
|
+
case _ => null
|
|
597
585
|
}
|
|
598
|
-
}
|
|
599
586
|
|
|
600
587
|
// Where a 307 or 308 points, when that is on the exchange's own origin. Any
|
|
601
588
|
// other redirect would drop the body or leave the origin.
|
|
@@ -45,6 +45,7 @@ class TelemetryFeature extends BaseFeature("telemetry", "0.0.1", true) {
|
|
|
45
45
|
val span = new LinkedHashMap[String, Object]()
|
|
46
46
|
span.put("traceId", id("trace"))
|
|
47
47
|
span.put("spanId", id("span"))
|
|
48
|
+
span.put("correlationId", ctx.id)
|
|
48
49
|
span.put("name", entity + "." + opname)
|
|
49
50
|
span.put("start", java.lang.Long.valueOf(FeatureOptions.foptNow(this.options).getAsLong()))
|
|
50
51
|
ctx.out.put(TELEMETRY_SPAN_KEY, span)
|
|
@@ -428,6 +428,34 @@ object SdkTestMain {
|
|
|
428
428
|
(sent.get(1).get("headers") eq b.ctx.response.headers),
|
|
429
429
|
"the cache handed on a copy it had read")
|
|
430
430
|
eqI("cache.notJson.bypass", 2, f.bypass) }
|
|
431
|
+
{ val rec = new FhRecorder()
|
|
432
|
+
val h = fhMake((c, u, fd) => rec.fetch(c, u, fd), fhF(new CacheFeature(), om("ttl" -> I(10000))))
|
|
433
|
+
h.op(fhOp("load").withPath("/w/1").withHeaders(om("x-tenant" -> "tenant-a")))
|
|
434
|
+
h.op(fhOp("load").withPath("/w/1").withHeaders(om("X-Tenant" -> "tenant-b")))
|
|
435
|
+
eqI("cache.tenantHeader", 2, rec.calls.size())
|
|
436
|
+
h.op(fhOp("load").withPath("/w/1").withHeaders(om("x-tenant" -> "tenant-b")))
|
|
437
|
+
eqI("cache.sameHeaders", 2, rec.calls.size()) }
|
|
438
|
+
{ val rec = new FhRecorder(); val f = new CacheFeature()
|
|
439
|
+
val h = fhMake((c, u, fd) => rec.fetch(c, u, fd), fhF(f, om("ttl" -> I(10000))))
|
|
440
|
+
val paths = Seq("/w/1", "/w", "/w/1/part", "/other/1", "/w/2")
|
|
441
|
+
paths.foreach(p => h.op(fhOp("load").withPath(p)))
|
|
442
|
+
h.op(fhOp("update").withMethod("PUT").withPath("/w/1"))
|
|
443
|
+
paths.foreach(p => h.op(fhOp("load").withPath(p)))
|
|
444
|
+
eqI("cache.writeDrops", 9, rec.calls.size()); eqI("cache.writeKeepsOther", 2, f.hit) }
|
|
445
|
+
for (control <- Seq("no-store", "private", "No-Cache", "max-age=60, private")) {
|
|
446
|
+
val rec = new FhRecorder()
|
|
447
|
+
rec.reply = (n, fd) => fhResponse(200, om("ok" -> B(true)), om("Cache-Control" -> control))
|
|
448
|
+
val h = fhMake((c, u, fd) => rec.fetch(c, u, fd), fhF(new CacheFeature(), om("ttl" -> I(10000))))
|
|
449
|
+
h.op(fhOp("load").withPath("/w")); h.op(fhOp("load").withPath("/w"))
|
|
450
|
+
eqI("cache.unstored " + control, 2, rec.calls.size())
|
|
451
|
+
}
|
|
452
|
+
{ val clk = new FhClock(); val rec = new FhRecorder()
|
|
453
|
+
rec.reply = (n, fd) => fhResponse(200, om("ok" -> B(true)), om("cache-control" -> "max-age=1"))
|
|
454
|
+
val h = fhMake((c, u, fd) => rec.fetch(c, u, fd), fhF(new CacheFeature(), om("ttl" -> I(10000), "now" -> f0(clk.nowFn))))
|
|
455
|
+
h.op(fhOp("load").withPath("/w")); clk.advance(500); h.op(fhOp("load").withPath("/w"))
|
|
456
|
+
eqI("cache.insideMaxAge", 1, rec.calls.size())
|
|
457
|
+
clk.advance(600); h.op(fhOp("load").withPath("/w"))
|
|
458
|
+
eqI("cache.maxAge", 2, rec.calls.size()) }
|
|
431
459
|
}
|
|
432
460
|
|
|
433
461
|
private def testIdempotency(): Unit = {
|
|
@@ -541,6 +569,28 @@ object SdkTestMain {
|
|
|
541
569
|
h.op(fhOp("load")); eqI("audit.inactive", 0, f.records.size()) }
|
|
542
570
|
}
|
|
543
571
|
|
|
572
|
+
private def testCorrelation(): Unit = {
|
|
573
|
+
{ val telemetry = new TelemetryFeature(); val debug = new DebugFeature(); val audit = new AuditFeature()
|
|
574
|
+
val h = fhMake(null, fhF(telemetry, null), fhF(debug, null), fhF(audit, null))
|
|
575
|
+
h.op(fhOp("load")); h.op(fhOp("list"))
|
|
576
|
+
def ids(recs: JList[JMap[String, Object]]): JList[Object] = {
|
|
577
|
+
val out = new ArrayList[Object]()
|
|
578
|
+
val it = recs.iterator()
|
|
579
|
+
while (it.hasNext) out.add(it.next().get("correlationId"))
|
|
580
|
+
out
|
|
581
|
+
}
|
|
582
|
+
val calls = ids(audit.records)
|
|
583
|
+
eqI("correlation.calls", 2, calls.size())
|
|
584
|
+
val it = calls.iterator()
|
|
585
|
+
while (it.hasNext) {
|
|
586
|
+
val id = it.next()
|
|
587
|
+
check("correlation.id", id.isInstanceOf[String] && "" != id, "expected a context id, got " + calls)
|
|
588
|
+
}
|
|
589
|
+
check("correlation.distinct", 2 == calls.size() && !jeq(calls.get(0), calls.get(1)), "expected a different id per call, got " + calls)
|
|
590
|
+
eq("correlation.spans", calls, ids(telemetry.spans))
|
|
591
|
+
eq("correlation.debug", calls, ids(debug.entries)) }
|
|
592
|
+
}
|
|
593
|
+
|
|
544
594
|
private def testClienttrack(): Unit = {
|
|
545
595
|
{ val rec = new FhRecorder(); val f = new ClienttrackFeature()
|
|
546
596
|
val h = fhMake((c, u, fd) => rec.fetch(c, u, fd), fhF(f, om("clientName" -> "Acme", "clientVersion" -> "2.0.0")))
|
|
@@ -1048,7 +1098,8 @@ object SdkTestMain {
|
|
|
1048
1098
|
"netsim" -> testNetsim, "retry" -> testRetry, "timeout" -> testTimeout,
|
|
1049
1099
|
"ratelimit" -> testRatelimit, "cache" -> testCache, "idempotency" -> testIdempotency,
|
|
1050
1100
|
"rbac" -> testRbac, "metrics" -> testMetrics, "telemetry" -> testTelemetry,
|
|
1051
|
-
"debug" -> testDebug, "audit" -> testAudit, "
|
|
1101
|
+
"debug" -> testDebug, "audit" -> testAudit, "correlation" -> testCorrelation,
|
|
1102
|
+
"clienttrack" -> testClienttrack,
|
|
1052
1103
|
"paging" -> testPaging, "streaming" -> testStreaming, "proxy" -> testProxy,
|
|
1053
1104
|
"composition" -> testComposition, "featureAdd" -> testFeatureAdd,
|
|
1054
1105
|
"featureOrder" -> testFeatureOrder, "entityCrud" -> testEntityCrud,
|
|
@@ -174,6 +174,11 @@ object Fetcher {
|
|
|
174
174
|
val reqb =
|
|
175
175
|
try HttpRequest.newBuilder(URI.create(fullurl)).method(method, bodyPublisher)
|
|
176
176
|
catch { case e: RuntimeException => throw new RuntimeException("fetch: invalid url: " + fullurl, e) }
|
|
177
|
+
// A bound in milliseconds, for a caller beneath the timeout feature.
|
|
178
|
+
fetchdef.get("timeout") match {
|
|
179
|
+
case n: java.lang.Number if 0 < n.longValue() => reqb.timeout(java.time.Duration.ofMillis(n.longValue()))
|
|
180
|
+
case _ =>
|
|
181
|
+
}
|
|
177
182
|
|
|
178
183
|
var hasUA = false
|
|
179
184
|
fetchdef.get("headers") match {
|
|
@@ -475,10 +475,12 @@ object MakeOptions {
|
|
|
475
475
|
}
|
|
476
476
|
|
|
477
477
|
val derived = new LinkedHashMap[String, Object]()
|
|
478
|
-
derived.put("clean", cleancfg)
|
|
479
478
|
derived.put("featureorder", featureorder)
|
|
480
479
|
opts.put("__derived__", derived)
|
|
481
480
|
|
|
481
|
+
// Beside the options, not in them: optionsMap hands them to callers.
|
|
482
|
+
ctx.cleancfg = cleancfg
|
|
483
|
+
|
|
482
484
|
// Again over the merged result: the config's own defaults can carry one.
|
|
483
485
|
addSensitiveOptions(cleancfg, opts, "clean", "__derived__")
|
|
484
486
|
|
|
@@ -83,15 +83,12 @@ object Clean {
|
|
|
83
83
|
)
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
// A context without
|
|
87
|
-
// the schema defaults.
|
|
86
|
+
// A context without a registry (makeError accepts a bare one) still masks
|
|
87
|
+
// by the schema defaults.
|
|
88
88
|
private def cleanConfig(ctx: Context): CleanConfig = {
|
|
89
|
-
if (ctx != null
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
case c: CleanConfig => return c
|
|
93
|
-
case _ =>
|
|
94
|
-
}
|
|
89
|
+
if (ctx != null) ctx.cleancfg match {
|
|
90
|
+
case c: CleanConfig => return c
|
|
91
|
+
case _ =>
|
|
95
92
|
}
|
|
96
93
|
makeCleanConfig(Schema.optspec.get("clean"))
|
|
97
94
|
}
|
|
@@ -24,6 +24,8 @@ public final class Context {
|
|
|
24
24
|
public var config: VMap?
|
|
25
25
|
public var entopts: VMap?
|
|
26
26
|
public var options: VMap?
|
|
27
|
+
// The clean registry, beside the options rather than in them.
|
|
28
|
+
public var cleancfg: Utility.CleanConfig?
|
|
27
29
|
public var opmap: [String: Operation] = [:]
|
|
28
30
|
public var response: Response?
|
|
29
31
|
public var result: Result?
|
|
@@ -79,6 +81,7 @@ public final class Context {
|
|
|
79
81
|
// Options
|
|
80
82
|
if let om = ctxProp(ctxmap, "options") as? VMap { options = om }
|
|
81
83
|
if options == nil, let b = basectx { options = b.options }
|
|
84
|
+
cleancfg = (ctxProp(ctxmap, "cleancfg") as? Utility.CleanConfig) ?? basectx?.cleancfg
|
|
82
85
|
|
|
83
86
|
// Entity
|
|
84
87
|
if let ent = ctxProp(ctxmap, "entity") as? Entity { entity = ent }
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
// Response caching for safe (read) requests
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
// first) and every hit/miss/bypass is counted. Bodies are snapshotted on
|
|
7
|
-
// capture so both the current caller and later hits can re-read the JSON body
|
|
8
|
-
// 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
|
+
// Foundation on Linux has no HMAC: the seeded Hasher over a random per-client
|
|
4
|
+
// salt separates header sets within one store without holding a header value.
|
|
5
|
+
// It is not a cryptographic commitment.
|
|
9
6
|
|
|
10
7
|
import Foundation
|
|
11
8
|
|
|
@@ -14,6 +11,10 @@ public final class CacheFeature: BaseFeature {
|
|
|
14
11
|
private var options: VMap?
|
|
15
12
|
private var store: [String: CacheEntry] = [:]
|
|
16
13
|
private var order: [String] = []
|
|
14
|
+
private var salt: [UInt8] = []
|
|
15
|
+
|
|
16
|
+
private static let safeMethods: Set<String> = ["GET", "HEAD"]
|
|
17
|
+
private static let unstored: Set<String> = ["no-store", "private", "no-cache"]
|
|
17
18
|
|
|
18
19
|
// Activity tracking (mirrors the ts client._cache record).
|
|
19
20
|
public var hit = 0
|
|
@@ -22,6 +23,7 @@ public final class CacheFeature: BaseFeature {
|
|
|
22
23
|
|
|
23
24
|
private final class CacheEntry {
|
|
24
25
|
var expiry: Int64 = 0
|
|
26
|
+
var scope: String = ""
|
|
25
27
|
var snapshot: CacheSnapshot = CacheSnapshot()
|
|
26
28
|
}
|
|
27
29
|
|
|
@@ -50,6 +52,8 @@ public final class CacheFeature: BaseFeature {
|
|
|
50
52
|
|
|
51
53
|
store = [:]
|
|
52
54
|
order = []
|
|
55
|
+
var rng = SystemRandomNumberGenerator()
|
|
56
|
+
salt = (0..<32).map { _ in UInt8.random(in: 0...255, using: &rng) }
|
|
53
57
|
|
|
54
58
|
let inner = ctx.utility!.fetcher!
|
|
55
59
|
|
|
@@ -68,10 +72,14 @@ public final class CacheFeature: BaseFeature {
|
|
|
68
72
|
let methods = foptStrList(options, "methods") ?? ["GET"]
|
|
69
73
|
let cacheable = methods.contains { $0.uppercased() == method }
|
|
70
74
|
if !cacheable {
|
|
75
|
+
if CacheFeature.safeMethods.contains(method) {
|
|
76
|
+
return try inner(ctx, url, fetchdef)
|
|
77
|
+
}
|
|
78
|
+
defer { invalidate(url) }
|
|
71
79
|
return try inner(ctx, url, fetchdef)
|
|
72
80
|
}
|
|
73
81
|
|
|
74
|
-
let key = method + " " + url
|
|
82
|
+
let key = method + " " + url + " " + digest(fetchdef.entries["headers"])
|
|
75
83
|
let now = foptNow(options)()
|
|
76
84
|
|
|
77
85
|
if let entry = store[key], entry.expiry > now {
|
|
@@ -95,10 +103,16 @@ public final class CacheFeature: BaseFeature {
|
|
|
95
103
|
|
|
96
104
|
if err == nil && CacheFeature.storable(res) {
|
|
97
105
|
let snap = CacheFeature.snapshot(res)
|
|
98
|
-
let ttl = foptInt(options, "ttl", 5000)
|
|
106
|
+
let ttl = CacheFeature.lifetime(snap.headers, Int64(foptInt(options, "ttl", 5000)))
|
|
107
|
+
if ttl <= 0 {
|
|
108
|
+
bypass += 1
|
|
109
|
+
return CacheFeature.replay(snap)
|
|
110
|
+
}
|
|
111
|
+
drop(key)
|
|
99
112
|
evict()
|
|
100
113
|
let entry = CacheEntry()
|
|
101
|
-
entry.expiry = now +
|
|
114
|
+
entry.expiry = now + ttl
|
|
115
|
+
entry.scope = CacheFeature.scope(url)
|
|
102
116
|
entry.snapshot = snap
|
|
103
117
|
store[key] = entry
|
|
104
118
|
order.append(key)
|
|
@@ -113,6 +127,103 @@ public final class CacheFeature: BaseFeature {
|
|
|
113
127
|
return res
|
|
114
128
|
}
|
|
115
129
|
|
|
130
|
+
private func digest(_ headers: Value?) -> String {
|
|
131
|
+
var pairs: [(String, String)] = []
|
|
132
|
+
if let hm = headers?.asMap {
|
|
133
|
+
for (k, v) in hm.entries {
|
|
134
|
+
if let text = CacheFeature.headerText(v) {
|
|
135
|
+
pairs.append((k.lowercased(), text))
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
pairs.sort { $0.0 != $1.0 ? $0.0 < $1.0 : $0.1 < $1.1 }
|
|
140
|
+
var hasher = Hasher()
|
|
141
|
+
hasher.combine(salt)
|
|
142
|
+
for (k, v) in pairs {
|
|
143
|
+
hasher.combine(k + ":" + v + "\n")
|
|
144
|
+
}
|
|
145
|
+
return String(UInt64(bitPattern: Int64(hasher.finalize())), radix: 16)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private static func headerText(_ v: Value) -> String? {
|
|
149
|
+
switch v {
|
|
150
|
+
case .noval, .null: return nil
|
|
151
|
+
case .string(let s): return s
|
|
152
|
+
case .int(let n): return String(n)
|
|
153
|
+
case .bool(let b): return String(b)
|
|
154
|
+
default: return "\(v)"
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private func invalidate(_ url: String) {
|
|
159
|
+
let path = CacheFeature.scope(url)
|
|
160
|
+
let collection = CacheFeature.parent(path)
|
|
161
|
+
for (key, entry) in store
|
|
162
|
+
where CacheFeature.under(entry.scope, path) || entry.scope == collection {
|
|
163
|
+
drop(key)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private func drop(_ key: String) {
|
|
168
|
+
if store.removeValue(forKey: key) != nil {
|
|
169
|
+
order.removeAll { $0 == key }
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// The configured ttl capped by the response's Cache-Control; zero when the
|
|
174
|
+
// response may not be stored.
|
|
175
|
+
private static func lifetime(_ headers: VMap, _ ttl: Int64) -> Int64 {
|
|
176
|
+
var control = ""
|
|
177
|
+
for (k, v) in headers.entries where k.lowercased() == "cache-control" {
|
|
178
|
+
control = (headerText(v) ?? "").lowercased()
|
|
179
|
+
}
|
|
180
|
+
var life = ttl
|
|
181
|
+
for part in control.split(separator: ",", omittingEmptySubsequences: false) {
|
|
182
|
+
let directive = part.trimmingCharacters(in: .whitespaces)
|
|
183
|
+
if unstored.contains(directive) {
|
|
184
|
+
return 0
|
|
185
|
+
}
|
|
186
|
+
if directive.hasPrefix("max-age") {
|
|
187
|
+
var value = directive.dropFirst("max-age".count).trimmingCharacters(in: .whitespaces)
|
|
188
|
+
if value.hasPrefix("=") {
|
|
189
|
+
value = value.dropFirst().trimmingCharacters(in: .whitespaces)
|
|
190
|
+
.trimmingCharacters(in: CharacterSet(charactersIn: "\""))
|
|
191
|
+
if !value.isEmpty, value.count < 16, value.allSatisfy({ $0.isASCII && $0.isNumber }),
|
|
192
|
+
let secs = Int64(value) {
|
|
193
|
+
life = min(life, secs * 1000)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return life
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private static func scope(_ url: String) -> String {
|
|
202
|
+
var out = String(url.split(maxSplits: 1, omittingEmptySubsequences: false) { $0 == "?" || $0 == "#" }[0])
|
|
203
|
+
while out.hasSuffix("/") {
|
|
204
|
+
out.removeLast()
|
|
205
|
+
}
|
|
206
|
+
return out
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private static func parent(_ scope: String) -> String {
|
|
210
|
+
let chars = Array(scope)
|
|
211
|
+
var root = 0
|
|
212
|
+
if let scheme = scope.range(of: "://") {
|
|
213
|
+
let after = scope.distance(from: scope.startIndex, to: scheme.upperBound)
|
|
214
|
+
guard let slash = chars[after...].firstIndex(of: "/") else {
|
|
215
|
+
return scope
|
|
216
|
+
}
|
|
217
|
+
root = slash
|
|
218
|
+
}
|
|
219
|
+
let cut = chars.lastIndex(of: "/") ?? 0
|
|
220
|
+
return String(chars[0..<Swift.max(cut, root)])
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private static func under(_ scope: String, _ prefix: String) -> Bool {
|
|
224
|
+
return scope == prefix || scope.hasPrefix(prefix + "/")
|
|
225
|
+
}
|
|
226
|
+
|
|
116
227
|
// A body the transport could not read as JSON passes through, for the operation to report.
|
|
117
228
|
private static func storable(_ res: Value) -> Bool {
|
|
118
229
|
let (status, has) = fresStatus(res)
|
|
@@ -40,6 +40,7 @@ public final class DebugFeature: BaseFeature {
|
|
|
40
40
|
|
|
41
41
|
let entry = VMap()
|
|
42
42
|
entry.entries["op"] = .string(entity + "." + opname)
|
|
43
|
+
entry.entries["correlationId"] = .string(ctx.id)
|
|
43
44
|
entry.entries["start"] = .int(foptNow(options)())
|
|
44
45
|
if let spec = ctx.spec {
|
|
45
46
|
entry.entries["method"] = .string(spec.method)
|
|
@@ -72,6 +72,7 @@ import VoxgigPlugin
|
|
|
72
72
|
|
|
73
73
|
public final class SecretsFeature: BaseFeature {
|
|
74
74
|
private var client: ProjectNameSDK?
|
|
75
|
+
private var rootctx: Context?
|
|
75
76
|
private var fopts: VMap?
|
|
76
77
|
|
|
77
78
|
// The LIVE options map (the root context's options - the same instance
|
|
@@ -175,6 +176,7 @@ public final class SecretsFeature: BaseFeature {
|
|
|
175
176
|
// Sync by feature contract: build the chain, never look anything up here.
|
|
176
177
|
public override func initFeature(_ ctx: Context, _ options: VMap) {
|
|
177
178
|
client = ctx.client
|
|
179
|
+
rootctx = ctx
|
|
178
180
|
fopts = options
|
|
179
181
|
liveopts = ctx.options ?? VMap()
|
|
180
182
|
active = foptBool(options, "active", false)
|
|
@@ -652,21 +654,15 @@ public final class SecretsFeature: BaseFeature {
|
|
|
652
654
|
fetchdef.entries["body"] = .string(body)
|
|
653
655
|
// Never followed by the client: a redirect would carry the refresh token on.
|
|
654
656
|
fetchdef.entries["redirect"] = .string("manual")
|
|
657
|
+
// Its own bound, in milliseconds: the timeout feature's wrapper is above it.
|
|
658
|
+
fetchdef.entries["timeout"] = .int(30000)
|
|
655
659
|
|
|
656
|
-
//
|
|
657
|
-
//
|
|
658
|
-
// the first expiry
|
|
659
|
-
|
|
660
|
-
// is the SAME raw HTTP path the SDK uses, taken BELOW the mode/test
|
|
661
|
-
// gate, so the ordinary case needs no custom transport (and no
|
|
662
|
-
// rawExchangeFetch twin, unlike go); a supplied system.fetch is honoured
|
|
663
|
-
// so a test can intercept the purchase.
|
|
664
|
-
let sysFetch = gpath(options, "system", "fetch")
|
|
660
|
+
// The SDK's own transport, beneath every feature's wrapper: it holds the
|
|
661
|
+
// test feature's block and the GET-only guard, and the wrapped one would
|
|
662
|
+
// recurse on the first expiry.
|
|
663
|
+
let rootctx = self.rootctx!
|
|
665
664
|
func fetch(_ at: String) throws -> Value {
|
|
666
|
-
|
|
667
|
-
return fn(at, fetchdef)
|
|
668
|
-
}
|
|
669
|
-
return try defaultHttpFetch(at, fetchdef)
|
|
665
|
+
return try fetcherUtil(rootctx, at, fetchdef)
|
|
670
666
|
}
|
|
671
667
|
|
|
672
668
|
var res = try fetch(url)
|