@voxgig/sdkgen 4.48.0 → 4.49.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 (174) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/dist/tsconfig.tsbuildinfo +1 -1
  3. package/dist/utility.js +9 -1
  4. package/dist/utility.js.map +1 -1
  5. package/package.json +1 -1
  6. package/project/.sdk/model/feature/secrets.aontu +3 -0
  7. package/project/.sdk/src/cmp/c/Config_c.ts +6 -1
  8. package/project/.sdk/src/cmp/c/fragment/Entity.fragment.c +0 -10
  9. package/project/.sdk/src/cmp/clojure/fragment/Entity.fragment.clj +0 -9
  10. package/project/.sdk/src/cmp/csharp/fragment/EntityBase.fragment.cs +0 -11
  11. package/project/.sdk/src/cmp/go/fragment/Entity.fragment.go +0 -7
  12. package/project/.sdk/src/cmp/java/fragment/EntityBase.fragment.java +0 -10
  13. package/project/.sdk/src/cmp/js/fragment/EntityBase.fragment.js +2 -11
  14. package/project/.sdk/src/cmp/kotlin/fragment/EntityBase.fragment.kt +0 -10
  15. package/project/.sdk/src/cmp/lua/fragment/Entity.fragment.lua +0 -11
  16. package/project/.sdk/src/cmp/ocaml/Entity_ocaml.ts +2 -10
  17. package/project/.sdk/src/cmp/perl/fragment/Entity.fragment.pm +0 -13
  18. package/project/.sdk/src/cmp/php/fragment/Entity.fragment.php +0 -10
  19. package/project/.sdk/src/cmp/py/fragment/Entity.fragment.py +0 -10
  20. package/project/.sdk/src/cmp/rb/fragment/Entity.fragment.rb +0 -10
  21. package/project/.sdk/src/cmp/rust/fragment/Entity.fragment.rs +0 -12
  22. package/project/.sdk/src/cmp/scala/fragment/EntityBase.fragment.scala +0 -10
  23. package/project/.sdk/src/cmp/swift/fragment/EntityBase.fragment.swift +0 -9
  24. package/project/.sdk/src/cmp/ts/fragment/EntityBase.fragment.ts +0 -7
  25. package/project/.sdk/src/cmp/zig/fragment/Entity.fragment.zig +1 -14
  26. package/project/.sdk/tm/c/feature/cache.c +16 -1
  27. package/project/.sdk/tm/c/feature/secrets.c +149 -18
  28. package/project/.sdk/tm/c/tests/feature/secrets/secrets_test.c +319 -70
  29. package/project/.sdk/tm/c/tests/feature_test.c +31 -0
  30. package/project/.sdk/tm/c/utility/make_spec.c +1 -1
  31. package/project/.sdk/tm/c/utility/prepare_body.c +64 -1
  32. package/project/.sdk/tm/c/utility/prepare_query.c +11 -6
  33. package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +74 -29
  34. package/project/.sdk/tm/clojure/src/sdk/core.clj +48 -10
  35. package/project/.sdk/tm/clojure/src/sdk/features.clj +16 -2
  36. package/project/.sdk/tm/clojure/test/sdk/test/feature/secrets.clj +191 -48
  37. package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +16 -0
  38. package/project/.sdk/tm/cpp/core/types.hpp +1 -11
  39. package/project/.sdk/tm/cpp/feature/cache.hpp +14 -1
  40. package/project/.sdk/tm/cpp/feature/secrets.hpp +133 -18
  41. package/project/.sdk/tm/cpp/test/feature/secrets/secrets_test.cpp +175 -8
  42. package/project/.sdk/tm/cpp/test/feature_test.cpp +27 -0
  43. package/project/.sdk/tm/cpp/utility/pipeline.hpp +52 -4
  44. package/project/.sdk/tm/csharp/core/Control.cs +0 -5
  45. package/project/.sdk/tm/csharp/core/Point.cs +6 -0
  46. package/project/.sdk/tm/csharp/feature/CacheFeature.cs +16 -0
  47. package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +77 -38
  48. package/project/.sdk/tm/csharp/test/FeatureTest.cs +29 -0
  49. package/project/.sdk/tm/csharp/test/feature/secrets/SecretsFeatureTest.cs +315 -68
  50. package/project/.sdk/tm/csharp/utility/MakeSpec.cs +2 -2
  51. package/project/.sdk/tm/csharp/utility/PrepareBody.cs +42 -0
  52. package/project/.sdk/tm/csharp/utility/PrepareQuery.cs +13 -2
  53. package/project/.sdk/tm/elixir/lib/projectname/entity_base.ex +0 -13
  54. package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +32 -10
  55. package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +78 -22
  56. package/project/.sdk/tm/elixir/lib/projectname/utility.ex +62 -6
  57. package/project/.sdk/tm/elixir/test/feature/secrets/secrets_test.exs +178 -19
  58. package/project/.sdk/tm/elixir/test/feature_test.exs +20 -0
  59. package/project/.sdk/tm/go/core/target.go +8 -0
  60. package/project/.sdk/tm/go/feature/cache_feature.go +14 -1
  61. package/project/.sdk/tm/go/feature/secrets_feature.go +110 -15
  62. package/project/.sdk/tm/go/test/feature/secrets/secrets_feature_test.go +373 -87
  63. package/project/.sdk/tm/go/test/feature_test.go +32 -0
  64. package/project/.sdk/tm/go/utility/media.go +2 -2
  65. package/project/.sdk/tm/go/utility/prepare_body.go +37 -1
  66. package/project/.sdk/tm/go/utility/prepare_query.go +10 -1
  67. package/project/.sdk/tm/java/core/Control.java +0 -5
  68. package/project/.sdk/tm/java/core/Point.java +7 -0
  69. package/project/.sdk/tm/java/feature/CacheFeature.java +12 -0
  70. package/project/.sdk/tm/java/feature/SecretsFeature.java +104 -34
  71. package/project/.sdk/tm/java/test/FeatureTest.java +25 -0
  72. package/project/.sdk/tm/java/test/PrimaryUtilityTest.java +39 -0
  73. package/project/.sdk/tm/java/test/feature/secrets/SecretsFeatureTest.java +267 -53
  74. package/project/.sdk/tm/java/utility/Fetcher.java +18 -2
  75. package/project/.sdk/tm/java/utility/MakeSpec.java +1 -1
  76. package/project/.sdk/tm/java/utility/Media.java +2 -2
  77. package/project/.sdk/tm/java/utility/PrepareBody.java +50 -0
  78. package/project/.sdk/tm/java/utility/PrepareQuery.java +11 -1
  79. package/project/.sdk/tm/js/src/Point.js +2 -0
  80. package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +13 -0
  81. package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +79 -29
  82. package/project/.sdk/tm/js/src/utility/MakeSpecUtility.js +1 -1
  83. package/project/.sdk/tm/js/src/utility/PrepareBodyUtility.js +29 -0
  84. package/project/.sdk/tm/js/src/utility/PrepareQueryUtility.js +5 -3
  85. package/project/.sdk/tm/js/test/feature/secrets/Secrets.test.js +282 -93
  86. package/project/.sdk/tm/js/test/feature.test.js +17 -0
  87. package/project/.sdk/tm/kotlin/core/Control.kt +0 -5
  88. package/project/.sdk/tm/kotlin/core/Point.kt +7 -0
  89. package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +12 -0
  90. package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +84 -24
  91. package/project/.sdk/tm/kotlin/test/FeatureTest.kt +24 -0
  92. package/project/.sdk/tm/kotlin/test/PrimaryUtilityTest.kt +35 -0
  93. package/project/.sdk/tm/kotlin/test/feature/secrets/SecretsTest.kt +236 -54
  94. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +13 -1
  95. package/project/.sdk/tm/kotlin/utility/MakeSpecUrl.kt +1 -1
  96. package/project/.sdk/tm/kotlin/utility/Prepare.kt +40 -10
  97. package/project/.sdk/tm/lua/feature/cache_feature.lua +20 -0
  98. package/project/.sdk/tm/lua/feature/secrets_feature.lua +92 -15
  99. package/project/.sdk/tm/lua/test/feature/secrets/secrets_feature_test.lua +208 -43
  100. package/project/.sdk/tm/lua/test/feature_test.lua +18 -0
  101. package/project/.sdk/tm/lua/utility/clean.lua +1 -0
  102. package/project/.sdk/tm/lua/utility/make_spec.lua +1 -1
  103. package/project/.sdk/tm/lua/utility/prepare_body.lua +40 -0
  104. package/project/.sdk/tm/lua/utility/prepare_query.lua +11 -1
  105. package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +100 -14
  106. package/project/.sdk/tm/ocaml/sdk_features.ml +7 -1
  107. package/project/.sdk/tm/ocaml/sdk_runtime.ml +40 -5
  108. package/project/.sdk/tm/ocaml/test/feature/secrets/t_secrets.ml +143 -7
  109. package/project/.sdk/tm/ocaml/test/t_feature.ml +15 -0
  110. package/project/.sdk/tm/perl/feature/cache_feature.pm +13 -0
  111. package/project/.sdk/tm/perl/feature/secrets_feature.pm +87 -32
  112. package/project/.sdk/tm/perl/t/feature/secrets/secrets.t +177 -24
  113. package/project/.sdk/tm/perl/t/feature.t +24 -0
  114. package/project/.sdk/tm/perl/utility/make_spec.pm +1 -1
  115. package/project/.sdk/tm/perl/utility/prepare_body.pm +38 -4
  116. package/project/.sdk/tm/perl/utility/prepare_query.pm +7 -1
  117. package/project/.sdk/tm/php/feature/CacheFeature.php +13 -0
  118. package/project/.sdk/tm/php/feature/SecretsFeature.php +79 -23
  119. package/project/.sdk/tm/php/test/FeatureTest.php +23 -0
  120. package/project/.sdk/tm/php/test/feature/secrets/SecretsTest.php +258 -48
  121. package/project/.sdk/tm/php/utility/MakeSpec.php +2 -2
  122. package/project/.sdk/tm/php/utility/PrepareBody.php +31 -0
  123. package/project/.sdk/tm/php/utility/PrepareQuery.php +9 -2
  124. package/project/.sdk/tm/py/pkg/feature/cache_feature.py +7 -1
  125. package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +90 -29
  126. package/project/.sdk/tm/py/pkg/utility/make_spec.py +1 -1
  127. package/project/.sdk/tm/py/pkg/utility/prepare_body.py +26 -0
  128. package/project/.sdk/tm/py/pkg/utility/prepare_query.py +7 -2
  129. package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +313 -80
  130. package/project/.sdk/tm/py/test/test_feature.py +18 -0
  131. package/project/.sdk/tm/rb/feature/cache_feature.rb +12 -0
  132. package/project/.sdk/tm/rb/feature/secrets_feature.rb +68 -21
  133. package/project/.sdk/tm/rb/test/feature/secrets/secrets_feature_test.rb +214 -43
  134. package/project/.sdk/tm/rb/test/feature_test.rb +20 -0
  135. package/project/.sdk/tm/rb/utility/make_spec.rb +1 -1
  136. package/project/.sdk/tm/rb/utility/prepare_body.rb +31 -3
  137. package/project/.sdk/tm/rb/utility/prepare_query.rb +7 -1
  138. package/project/.sdk/tm/rust/feature/cache.rs +15 -1
  139. package/project/.sdk/tm/rust/feature/secrets.rs +103 -23
  140. package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +357 -67
  141. package/project/.sdk/tm/rust/tests/feature_test.rs +41 -0
  142. package/project/.sdk/tm/rust/utility/prepare_body.rs +51 -0
  143. package/project/.sdk/tm/rust/utility/prepare_query.rs +12 -4
  144. package/project/.sdk/tm/scala/core/Control.scala +0 -5
  145. package/project/.sdk/tm/scala/core/Point.scala +3 -0
  146. package/project/.sdk/tm/scala/feature/CacheFeature.scala +12 -0
  147. package/project/.sdk/tm/scala/feature/SecretsFeature.scala +88 -22
  148. package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +61 -1
  149. package/project/.sdk/tm/scala/sdktest/feature/secrets/SecretsTestMain.scala +230 -73
  150. package/project/.sdk/tm/scala/utility/Fetcher.scala +13 -1
  151. package/project/.sdk/tm/scala/utility/MakeSpec.scala +1 -1
  152. package/project/.sdk/tm/scala/utility/Prepare.scala +53 -12
  153. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Control.swift +0 -5
  154. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Point.swift +3 -0
  155. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +11 -0
  156. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +76 -16
  157. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Make.swift +1 -1
  158. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/Prepare.swift +36 -1
  159. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +21 -0
  160. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/feature/secrets/SecretsFeatureTest.swift +257 -73
  161. package/project/.sdk/tm/ts/src/Point.ts +3 -0
  162. package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +13 -0
  163. package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +77 -17
  164. package/project/.sdk/tm/ts/src/utility/PrepareBodyUtility.ts +29 -0
  165. package/project/.sdk/tm/ts/src/utility/PrepareQueryUtility.ts +5 -3
  166. package/project/.sdk/tm/ts/test/feature/secrets/Secrets.test.ts +282 -87
  167. package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
  168. package/project/.sdk/tm/zig/core/utility.zig +47 -4
  169. package/project/.sdk/tm/zig/feature/cache.zig +15 -1
  170. package/project/.sdk/tm/zig/feature/secrets.zig +109 -32
  171. package/project/.sdk/tm/zig/test/feature/secrets/secrets_test.zig +232 -9
  172. package/project/.sdk/tm/zig/test/feature_test.zig +33 -0
  173. package/project/sdkgen-package.json +1 -1
  174. package/src/utility.ts +12 -1
@@ -12,7 +12,7 @@
12
12
  // trims it, along with the feature source and the vendored library, for a
13
13
  // project whose model does not select `secrets`.
14
14
 
15
- const { test, describe, beforeEach } = require('node:test')
15
+ const { test, describe, beforeEach, after } = require('node:test')
16
16
  const assert = require('node:assert')
17
17
  const Http = require('node:http')
18
18
 
@@ -52,16 +52,67 @@ async function prepared(sdkopts) {
52
52
  }
53
53
 
54
54
 
55
- // The credential header carries the SPEC's credential prefix, which a
56
- // TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
57
- // `Bearer <token>`, an apiKey scheme the raw token. So assert on the
58
- // CREDENTIAL and let the prefix be whatever this SDK's API declares —
59
- // pinning the whole header value passes only for a prefix-less API, and
60
- // this file ships to every project that selects the feature.
61
- function credentialIs(header, token) {
62
- const got = String(null == header ? '' : header)
63
- assert.ok(got === token || got.endsWith(' ' + token),
64
- 'expected the ' + CREDHEADER + ' header to carry ' + token + ', got: ' + got)
55
+ // The form prepareAuth writes, from the DEFAULT client's auth: under Basic the
56
+ // token is the key half of the encoded pair, and a prefix is space-joined.
57
+ const CREDFORM = (() => {
58
+ const auth = SDK.test({}, {}).options().auth || {}
59
+ return { prefix: 'string' === typeof auth.prefix ? auth.prefix : '', basic: true === auth.basic }
60
+ })()
61
+
62
+
63
+ function credentialIs(header, token, secret) {
64
+ const value = CREDFORM.basic ?
65
+ Buffer.from(token + ':' + (secret || '')).toString('base64') : token
66
+ assert.equal(header, '' === CREDFORM.prefix ? value : CREDFORM.prefix + ' ' + value,
67
+ 'expected the ' + CREDHEADER + ' header to carry ' + token)
68
+ }
69
+
70
+
71
+ // A stub standing in for both endpoints the flow touches: the token
72
+ // endpoint, and everything else.
73
+ //
74
+ // `apiStatus` is a SCRIPT — one status per API call, the last repeating —
75
+ // so a case can say "401 then 200" without counting calls itself.
76
+ function stubfetch(opts) {
77
+ const o = opts || {}
78
+ const tokens = o.tokens || ['ACCESS01', 'ACCESS02', 'ACCESS03']
79
+ const apiStatus = o.apiStatus || [200]
80
+
81
+ const calls = []
82
+ let issued = 0
83
+ let apicall = -1
84
+
85
+ const fetch = async (url, init) => {
86
+ calls.push({ url, init, auth: (init.headers || {})[CREDHEADER] })
87
+
88
+ if (url.endsWith('/' + (o.path || 'auth/token'))) {
89
+ if (true === o.tokenfails) {
90
+ return { status: 401, json: async () => ({ error: 'nope' }), headers: {} }
91
+ }
92
+ const body = {}
93
+ body[o.response || 'access_token'] = tokens[Math.min(issued, tokens.length - 1)]
94
+ issued++
95
+ return { status: 200, json: async () => body, headers: {} }
96
+ }
97
+
98
+ apicall++
99
+ const status = apiStatus[Math.min(apicall, apiStatus.length - 1)]
100
+ return { status, json: async () => ({ ok: status < 400 }), headers: {} }
101
+ }
102
+
103
+ return {
104
+ fetch,
105
+ calls,
106
+ // Only the calls that went to the API, in order.
107
+ api: () => calls.filter((c) => !c.url.endsWith('/' + (o.path || 'auth/token'))),
108
+ token: () => calls.filter((c) => c.url.endsWith('/' + (o.path || 'auth/token'))),
109
+ }
110
+ }
111
+
112
+
113
+ // Same rule as credentialIs, reading the header off a recorded call.
114
+ function authIs(call, token, secret) {
115
+ credentialIs(null == call ? undefined : call.auth, token, secret)
65
116
  }
66
117
 
67
118
 
@@ -127,6 +178,13 @@ describe('secrets', { skip: NOAUTH }, () => {
127
178
  })
128
179
 
129
180
 
181
+ test('active: a resolved key is placed as prepareAuth places it, the secret kept', async () => {
182
+ process.env[ENVPREFIX + 'APIKEY'] = 'ENVKEY03'
183
+ const { fetchdef } = await prepared(Object.assign({ secret: 'PASS01' }, envchain()))
184
+ credentialIs(fetchdef.headers[CREDHEADER], 'ENVKEY03', 'PASS01')
185
+ })
186
+
187
+
130
188
  test('active: an explicitly EMPTY apikey also defers to the chain', async () => {
131
189
  process.env[ENVPREFIX + 'APIKEY'] = 'ENVKEY01'
132
190
  const { fetchdef } = await prepared(
@@ -619,56 +677,9 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
619
677
  })
620
678
 
621
679
 
622
- // A stub standing in for both endpoints the flow touches: the token
623
- // endpoint, and everything else.
624
- //
625
- // `apiStatus` is a SCRIPT — one status per API call, the last repeating —
626
- // so a case can say "401 then 200" without counting calls itself.
627
- function stubfetch(opts) {
628
- const o = opts || {}
629
- const tokens = o.tokens || ['ACCESS01', 'ACCESS02', 'ACCESS03']
630
- const apiStatus = o.apiStatus || [200]
631
-
632
- const calls = []
633
- let issued = 0
634
- let apicall = -1
635
-
636
- const fetch = async (url, init) => {
637
- calls.push({ url, init, auth: (init.headers || {})[CREDHEADER] })
638
-
639
- if (url.endsWith('/' + (o.path || 'auth/token'))) {
640
- if (true === o.tokenfails) {
641
- return { status: 401, json: async () => ({ error: 'nope' }), headers: {} }
642
- }
643
- const body = {}
644
- body[o.response || 'access_token'] = tokens[Math.min(issued, tokens.length - 1)]
645
- issued++
646
- return { status: 200, json: async () => body, headers: {} }
647
- }
648
-
649
- apicall++
650
- const status = apiStatus[Math.min(apicall, apiStatus.length - 1)]
651
- return { status, json: async () => ({ ok: status < 400 }), headers: {} }
652
- }
653
-
654
- return {
655
- fetch,
656
- calls,
657
- // Only the calls that went to the API, in order.
658
- api: () => calls.filter((c) => !c.url.endsWith('/' + (o.path || 'auth/token'))),
659
- token: () => calls.filter((c) => c.url.endsWith('/' + (o.path || 'auth/token'))),
660
- }
661
- }
662
-
663
-
664
- // Same rule as credentialIs, reading the header off a recorded call.
665
- function authIs(call, token) {
666
- credentialIs(null == call ? undefined : call.auth, token)
667
- }
668
-
669
-
670
- function exchangeSdk(stub, extra) {
680
+ function exchangeSdk(stub, extra, sdkopts) {
671
681
  return new SDK({
682
+ ...(sdkopts || {}),
672
683
  base: BASE,
673
684
  system: { fetch: stub.fetch },
674
685
  feature: {
@@ -769,6 +780,21 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
769
780
  })
770
781
 
771
782
 
783
+ test('a token bought on a refusal is placed as prepareAuth places it, the secret kept',
784
+ async () => {
785
+ process.env[ENVPREFIX + 'REFRESH_TOKEN'] = REFRESH
786
+
787
+ const stub = stubfetch({ apiStatus: [401, 200] })
788
+ const sdk = exchangeSdk(stub, {}, { secret: 'PASS01' })
789
+
790
+ await sdk.direct({ path: '/thing' })
791
+
792
+ assert.equal(stub.api().length, 2, 'expected the request to be retried')
793
+ authIs(stub.api()[0], 'ACCESS01', 'PASS01')
794
+ authIs(stub.api()[1], 'ACCESS02', 'PASS01')
795
+ })
796
+
797
+
772
798
  test('the retry happens once, not in a loop', async () => {
773
799
  process.env[ENVPREFIX + 'REFRESH_TOKEN'] = REFRESH
774
800
 
@@ -1039,30 +1065,31 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
1039
1065
  })
1040
1066
 
1041
1067
 
1042
- // Real listeners and the platform fetch: a stub follows nothing.
1043
- test('a redirect answering the exchange is a failure, and nothing follows it', async () => {
1044
- const elsewhere = []
1045
- const followed = []
1046
- let answer = [307, '']
1068
+ // Real listeners and the platform fetch: a stub follows nothing. The
1069
+ // origin answers the exchange as `answer` says and records what else it gets.
1070
+ async function withOrigins(run) {
1071
+ const o = { elsewhere: [], followed: [], exchanged: 0, answer: [307, ''] }
1047
1072
 
1048
1073
  const other = Http.createServer((req, res) => {
1049
1074
  let body = ''
1050
1075
  req.on('data', (chunk) => body += chunk)
1051
1076
  req.on('end', () => {
1052
- elsewhere.push({ method: req.method, url: req.url, body })
1077
+ o.elsewhere.push({ method: req.method, url: req.url, body })
1053
1078
  res.writeHead(200, { 'content-type': 'application/json', connection: 'close' })
1054
1079
  res.end('{"access_token":"ELSEWHERE01"}')
1055
1080
  })
1056
1081
  })
1057
1082
  const origin = Http.createServer((req, res) => {
1058
- req.resume()
1083
+ let body = ''
1084
+ req.on('data', (chunk) => body += chunk)
1059
1085
  req.on('end', () => {
1060
1086
  if ('/api/auth/token' === req.url) {
1061
- res.writeHead(answer[0], { location: answer[1], connection: 'close' })
1087
+ o.exchanged++
1088
+ res.writeHead(o.answer[0], { location: o.answer[1], connection: 'close' })
1062
1089
  res.end()
1063
1090
  return
1064
1091
  }
1065
- followed.push(req.method + ' ' + req.url)
1092
+ o.followed.push((req.method + ' ' + req.url + ' ' + body).trim())
1066
1093
  res.writeHead(200, { 'content-type': 'application/json', connection: 'close' })
1067
1094
  res.end('{"access_token":"MOVED01"}')
1068
1095
  })
@@ -1070,32 +1097,50 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
1070
1097
 
1071
1098
  const listen = (server) => new Promise((resolve) =>
1072
1099
  server.listen(0, '127.0.0.1', () => resolve(server.address().port)))
1073
- const base = 'http://127.0.0.1:' + (await listen(origin)) + '/api'
1074
- const away = 'http://127.0.0.1:' + (await listen(other)) + '/api/auth/token'
1100
+ o.origin = 'http://127.0.0.1:' + (await listen(origin))
1101
+ o.base = o.origin + '/api'
1102
+ o.away = 'http://127.0.0.1:' + (await listen(other)) + '/api/auth/token'
1075
1103
 
1076
1104
  try {
1077
- const answers = [[307, away], [308, away], [302, away], [307, '/api/moved/token']]
1105
+ await run(o)
1106
+ }
1107
+ finally {
1108
+ origin.close()
1109
+ other.close()
1110
+ }
1111
+ }
1112
+
1113
+
1114
+ function redirectSdk(o, exchange) {
1115
+ return new SDK({
1116
+ base: o.base,
1117
+ feature: {
1118
+ secrets: {
1119
+ active: true,
1120
+ name: 'refresh_token',
1121
+ exchange: Object.assign({ active: true, refresh: REFRESH }, exchange || {}),
1122
+ },
1123
+ },
1124
+ })
1125
+ }
1126
+
1127
+
1128
+ test('a redirect answering the exchange is a failure, and nothing follows it', async () => {
1129
+ await withOrigins(async (o) => {
1130
+ const answers =
1131
+ [[307, o.away], [308, o.away], [302, o.away], [307, '/api/moved/token']]
1078
1132
 
1079
1133
  const allowed = []
1080
1134
  let refused = 0
1081
1135
 
1082
- for (answer of answers) {
1083
- const sdk = new SDK({
1084
- base,
1085
- feature: {
1086
- secrets: {
1087
- active: true,
1088
- name: 'refresh_token',
1089
- exchange: { active: true, refresh: REFRESH },
1090
- },
1091
- },
1092
- })
1136
+ for (const answer of answers) {
1137
+ o.answer = answer
1093
1138
 
1094
- const res = await sdk.direct({ path: '/thing' })
1139
+ const res = await redirectSdk(o).direct({ path: '/thing' })
1095
1140
  const message = String(null == res || null == res.err ? res : res.err.message)
1096
1141
 
1097
1142
  if (message.includes(
1098
- 'token exchange failed: ' + answer[0] + ' from ' + base + '/auth/token')) {
1143
+ 'token exchange failed: ' + answer[0] + ' from ' + o.base + '/auth/token')) {
1099
1144
  refused++
1100
1145
  }
1101
1146
  else {
@@ -1104,17 +1149,161 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
1104
1149
  }
1105
1150
 
1106
1151
  console.log('secrets: exchange refused ' + refused + ' redirect(s), ' +
1107
- (elsewhere.length + followed.length) + ' followed')
1152
+ (o.elsewhere.length + o.followed.length) + ' followed')
1108
1153
 
1109
- assert.deepEqual(elsewhere, [], 'the exchange reached another origin')
1110
- assert.deepEqual(followed, [], 'a request followed a refused exchange')
1154
+ assert.deepEqual(o.elsewhere, [], 'the exchange reached another origin')
1155
+ assert.deepEqual(o.followed, [], 'a request followed a refused exchange')
1111
1156
  assert.deepEqual(allowed, [], 'a redirect was not a failed exchange')
1112
1157
  assert.equal(refused, answers.length)
1113
- }
1114
- finally {
1115
- origin.close()
1116
- other.close()
1117
- }
1158
+ })
1159
+ })
1160
+
1161
+
1162
+ test('with the option, a redirect on the exchange\'s own origin is followed, and no other', async () => {
1163
+ await withOrigins(async (o) => {
1164
+ const sent = ' {"refresh_token":"' + REFRESH + '"}'
1165
+ // Each case: the status, the Location, and the path the exchange is
1166
+ // asked again at, or null where the redirect is refused.
1167
+ const cases = [
1168
+ [307, '/api/moved/token', '/api/moved/token'],
1169
+ [308, o.origin + '/api/moved/token', '/api/moved/token'],
1170
+ // A Location of a query alone keeps the path that answered.
1171
+ [307, '?step=2', '/api/auth/token?step=2'],
1172
+ [307, o.away, null],
1173
+ [302, '/api/moved/token', null],
1174
+ [307, '/api/auth/token', null],
1175
+ ]
1176
+
1177
+ const wrong = []
1178
+ let followed = 0
1179
+ let refused = 0
1180
+ let looped = 0
1181
+
1182
+ for (const [status, location, path] of cases) {
1183
+ o.answer = [status, location]
1184
+ o.followed.length = 0
1185
+ o.exchanged = 0
1186
+
1187
+ const res = await redirectSdk(o, { redirect: 'same-origin' }).direct({ path: '/thing' })
1188
+ const message = String(null == res || null == res.err ? res : res.err.message)
1189
+ const seen = status + ' to ' + location + ': ' + message + ' ' + JSON.stringify(o.followed)
1190
+
1191
+ if (null != path) {
1192
+ if (true === res.ok && 'POST ' + path + sent === o.followed[0] &&
1193
+ 2 === o.followed.length) followed++
1194
+ else wrong.push(seen)
1195
+ }
1196
+ else if (0 === o.followed.length && message.includes(
1197
+ 'token exchange failed: ' + status + ' from ' + o.base + '/auth/token')) {
1198
+ refused++
1199
+ }
1200
+ else {
1201
+ wrong.push(seen)
1202
+ }
1203
+
1204
+ if ('/api/auth/token' === location) looped = o.exchanged
1205
+ }
1206
+
1207
+ console.log('secrets: exchange opt-in followed ' + followed +
1208
+ ' redirect(s) on its own origin, refused ' + refused + ', ' +
1209
+ o.elsewhere.length + ' elsewhere')
1210
+
1211
+ assert.deepEqual(o.elsewhere, [], 'the exchange reached another origin')
1212
+ assert.deepEqual(wrong, [])
1213
+ assert.equal(followed, 3)
1214
+ assert.equal(refused, 3)
1215
+ // A redirect to itself stops at the hop limit.
1216
+ assert.equal(looped, 6)
1217
+ })
1218
+ })
1219
+
1220
+ })
1221
+
1222
+
1223
+ // Off by the config where it declares no auth, else by `auth: null`: either
1224
+ // way the feature places no credential, buys no token and retries nothing.
1225
+ const AUTHOFF = NOAUTH ? { by: 'config', opts: {} } : { by: 'option', opts: { auth: null } }
1226
+
1227
+
1228
+ describe('secrets with auth off', () => {
1229
+
1230
+ const BASE = 'http://authoff.test/api'
1231
+ const SECRETS = ['OFFKEY01', 'OFFKEY02', 'OFFREFRESH01', 'ACCESS01']
1232
+ const sent = []
1233
+ let bought = 0
1234
+
1235
+
1236
+ beforeEach(() => {
1237
+ delete process.env[ENVPREFIX + 'APIKEY']
1238
+ delete process.env[ENVPREFIX + 'REFRESH_TOKEN']
1239
+ })
1240
+
1241
+
1242
+ after(() => {
1243
+ console.log('secrets: auth off by ' + AUTHOFF.by + ': ' + sent.length + ' request(s), ' +
1244
+ sent.filter(carries).length + ' credential(s), ' + bought + ' token(s)')
1245
+ })
1246
+
1247
+
1248
+ // The header prepareAuth writes, or any header holding one of the secrets.
1249
+ function carries(call) {
1250
+ const headers = call.init.headers || {}
1251
+ return undefined !== headers[CREDHEADER] || Object.values(headers)
1252
+ .some((v) => SECRETS.some((secret) => String(v).includes(secret)))
1253
+ }
1254
+
1255
+
1256
+ async function offsdk(stub, sdkopts, fopts) {
1257
+ const sdk = new SDK({
1258
+ ...sdkopts,
1259
+ ...AUTHOFF.opts,
1260
+ base: BASE,
1261
+ system: { fetch: stub.fetch },
1262
+ feature: {
1263
+ secrets: {
1264
+ active: true,
1265
+ providers: [{ kind: 'env', prefix: ENVPREFIX }],
1266
+ ...fopts,
1267
+ },
1268
+ },
1269
+ })
1270
+ await sdk.direct({ path: '/thing' })
1271
+ sent.push(...stub.api())
1272
+ bought += stub.token().length
1273
+ return sdk
1274
+ }
1275
+
1276
+
1277
+ test('a key the chain resolves is not placed', async () => {
1278
+ process.env[ENVPREFIX + 'APIKEY'] = 'OFFKEY01'
1279
+
1280
+ const stub = stubfetch()
1281
+ const sdk = await offsdk(stub, {}, {})
1282
+
1283
+ assert.equal(stub.api().length, 1)
1284
+ assert.ok(!carries(stub.api()[0]), 'a credential was sent with auth off')
1285
+ assert.equal(sdk.options().apikey, '', 'the resolved key was placed where prepareAuth reads it')
1286
+ })
1287
+
1288
+
1289
+ test('an explicit apikey is not placed', async () => {
1290
+ const stub = stubfetch()
1291
+ await offsdk(stub, { apikey: 'OFFKEY02' }, {})
1292
+
1293
+ assert.equal(stub.api().length, 1)
1294
+ assert.ok(!carries(stub.api()[0]), 'a credential was sent with auth off')
1295
+ })
1296
+
1297
+
1298
+ test('a refusal buys no token and is not retried', async () => {
1299
+ process.env[ENVPREFIX + 'REFRESH_TOKEN'] = 'OFFREFRESH01'
1300
+
1301
+ const stub = stubfetch({ apiStatus: [401] })
1302
+ await offsdk(stub, {}, { name: 'refresh_token', exchange: { active: true } })
1303
+
1304
+ assert.equal(stub.token().length, 0, 'a token was bought with auth off')
1305
+ assert.equal(stub.api().length, 1, 'a refusal was retried with auth off')
1306
+ assert.ok(!carries(stub.api()[0]), 'a credential was sent with auth off')
1118
1307
  })
1119
1308
 
1120
1309
  })
@@ -278,6 +278,23 @@ describe('feature', () => {
278
278
  await h.op({ op: 'load', path: '/a' }) // miss again
279
279
  strictEqual(rec.calls.length, 3)
280
280
  })
281
+
282
+ test('a body that declares another type than JSON passes by, unread', async () => {
283
+ const sent = []
284
+ const rec = recordingServer((_n) => {
285
+ const res = makeResponse(200, undefined, { 'content-type': 'audio/wav' })
286
+ res.text = async () => 'RIFF'
287
+ sent.push(res)
288
+ return res
289
+ })
290
+ const h = makeClient({ features: [{ name: 'cache' }], server: rec.server })
291
+ const a = await h.op({ op: 'load', path: '/w/1/audio' })
292
+ const b = await h.op({ op: 'load', path: '/w/1/audio' })
293
+ strictEqual(rec.calls.length, 2, 'the body was stored')
294
+ strictEqual(a.ctx.response, sent[0], 'the cache handed on a copy it had read')
295
+ strictEqual(b.ctx.response, sent[1])
296
+ strictEqual(h.client._cache.bypass, 2)
297
+ })
281
298
  })
282
299
 
283
300
 
@@ -13,9 +13,4 @@ class Control {
13
13
  var actor: String = ""
14
14
 
15
15
  var paging: MutableMap<String, Any?>? = null
16
-
17
- // Outbound streaming marker: the entity `stream` method sets this to the
18
- // caller-supplied streamable payload so the request builder / transport can
19
- // stream it as the request body.
20
- var streamOut: Any? = null
21
16
  }
@@ -19,6 +19,8 @@ class Point(pointmap: Map<String, Any?>?) {
19
19
  var relations: MutableList<Any?>? = null
20
20
  var alias: MutableMap<String, Any?> = linkedMapOf()
21
21
  var transform: MutableMap<String, Any?> = linkedMapOf()
22
+ // The fields a list, load or remove sends in a request body.
23
+ var fields: MutableList<Any?>? = null
22
24
 
23
25
  init {
24
26
  var v = Struct.getprop(pointmap, "args")
@@ -81,5 +83,10 @@ class Point(pointmap: Map<String, Any?>?) {
81
83
  if (v is MutableMap<*, *>) {
82
84
  this.transform = v as MutableMap<String, Any?>
83
85
  }
86
+
87
+ v = Struct.getprop(pointmap, "fields")
88
+ if (v is MutableList<*>) {
89
+ this.fields = v as MutableList<Any?>
90
+ }
84
91
  }
85
92
  }
@@ -5,6 +5,7 @@ import java.util.function.Supplier
5
5
  import KOTLINPACKAGE.core.Context
6
6
  import KOTLINPACKAGE.core.FetcherFn
7
7
  import KOTLINPACKAGE.core.SdkClient
8
+ import KOTLINPACKAGE.utility.Media
8
9
 
9
10
  // Response caching for safe (read) requests. Wraps the active transport and
10
11
  // serves a fresh cached snapshot instead of hitting the network when the same
@@ -91,6 +92,12 @@ class CacheFeature : BaseFeature("cache", "0.0.1", true) {
91
92
  throw err
92
93
  }
93
94
 
95
+ // A body that declares another type than JSON passes by, unread.
96
+ if (storable(res) && !json(res)) {
97
+ this.bypass++
98
+ return res
99
+ }
100
+
94
101
  if (storable(res)) {
95
102
  val snapshot = snapshot(res)
96
103
  val ttl = FeatureOptions.foptInt(this.options, "ttl", 5000)
@@ -114,6 +121,11 @@ class CacheFeature : BaseFeature("cache", "0.0.1", true) {
114
121
  return status in 200..299 && (res as Map<*, *>)["unreadable"] != true
115
122
  }
116
123
 
124
+ private fun json(res: Any?): Boolean {
125
+ val type = FeatureOptions.fresHeader(res, "content-type")
126
+ return "" == type || Media.isJsonMedia(type)
127
+ }
128
+
117
129
  private fun snapshot(res: Any?): CacheSnapshot {
118
130
  val rm = if (res is MutableMap<*, *>) res as MutableMap<String, Any?> else null
119
131