@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
@@ -26,6 +26,35 @@ function prepareBody(ctx: Context) {
26
26
  }
27
27
  }
28
28
 
29
+ else if (0 < (ctx.point?.fields?.length ?? 0)) {
30
+ body = matchBody(ctx)
31
+ }
32
+
33
+ return body
34
+ }
35
+
36
+
37
+ // A list, load or remove routed to a method with a body sends the match
38
+ // fields that body declares, never a path parameter, and never a header,
39
+ // cookie or query argument the body does not also declare as a field.
40
+ function matchBody(ctx: Context): Record<string, any> {
41
+ const point: any = ctx.point
42
+ const args = point.args || {}
43
+ const getprop = ctx.utility.struct.getprop
44
+
45
+ const routed: string[] = (point.params || [])
46
+ .concat((args.params || []).map((arg: any) => arg?.name))
47
+ .concat(['header', 'cookie', 'query'].flatMap((kind) => (args[kind] || [])
48
+ .filter((arg: any) => true !== arg?.field).map((arg: any) => arg?.name)))
49
+
50
+ const body: Record<string, any> = {}
51
+ for (const name of point.fields || []) {
52
+ const val = getprop(ctx.reqmatch, name)
53
+ if (null != val && '$action' !== name && !routed.includes(name)) {
54
+ body[name] = val
55
+ }
56
+ }
57
+
29
58
  return body
30
59
  }
31
60
 
@@ -34,10 +34,12 @@ function prepareQuery(ctx: Context) {
34
34
  }
35
35
  }
36
36
 
37
- // A header or cookie parameter travels in the headers, which prepareHeaders
38
- // fills, unless a query parameter shares its name: then both are sent.
37
+ // A header or cookie argument travels in the headers, and a field the point's
38
+ // body declares in the body, unless a query parameter shares its name.
39
39
  const elsewhere: string[] = (point.args?.header || []).concat(point.args?.cookie || [])
40
- .map((a: any) => a?.name).filter((name: string) => !declared.includes(name))
40
+ .map((a: any) => a?.name)
41
+ .concat(Array.isArray(point.fields) ? point.fields : [])
42
+ .filter((name: string) => !declared.includes(name))
41
43
 
42
44
  const out: any = {}
43
45
  for (let [key, val] of items(reqmatch)) {
@@ -1,5 +1,5 @@
1
1
 
2
- import { test, describe, beforeEach } from 'node:test'
2
+ import { test, describe, beforeEach, after } from 'node:test'
3
3
  import assert from 'node:assert'
4
4
  import Http from 'node:http'
5
5
 
@@ -39,10 +39,67 @@ async function prepared(sdkopts: any): Promise<any> {
39
39
  }
40
40
 
41
41
 
42
- function credentialIs(header: any, token: string) {
43
- const got = String(null == header ? '' : header)
44
- assert.ok(got === token || got.endsWith(' ' + token),
45
- 'expected the ' + CREDHEADER + ' header to carry ' + token + ', got: ' + got)
42
+ // The form prepareAuth writes, from the DEFAULT client's auth: under Basic the
43
+ // token is the key half of the encoded pair, and a prefix is space-joined.
44
+ const CREDFORM = (() => {
45
+ const auth = (SDK as any).test({}, {}).options().auth || {}
46
+ return { prefix: 'string' === typeof auth.prefix ? auth.prefix : '', basic: true === auth.basic }
47
+ })()
48
+
49
+
50
+ function credentialIs(header: any, token: string, secret?: string) {
51
+ const value = CREDFORM.basic ?
52
+ Buffer.from(token + ':' + (secret || '')).toString('base64') : token
53
+ assert.equal(header, '' === CREDFORM.prefix ? value : CREDFORM.prefix + ' ' + value,
54
+ 'expected the ' + CREDHEADER + ' header to carry ' + token)
55
+ }
56
+
57
+
58
+ // A stub standing in for both endpoints the flow touches: the token
59
+ // endpoint, and everything else.
60
+ //
61
+ // `apiStatus` is a SCRIPT — one status per API call, the last repeating —
62
+ // so a case can say "401 then 200" without counting calls itself.
63
+ function stubfetch(opts?: any) {
64
+ const o = opts || {}
65
+ const tokens: string[] = o.tokens || ['ACCESS01', 'ACCESS02', 'ACCESS03']
66
+ const apiStatus: number[] = o.apiStatus || [200]
67
+
68
+ const calls: any[] = []
69
+ let issued = 0
70
+ let apicall = -1
71
+
72
+ const fetch = async (url: string, init: any) => {
73
+ calls.push({ url, init, auth: (init.headers || {})[CREDHEADER] })
74
+
75
+ if (url.endsWith('/' + (o.path || 'auth/token'))) {
76
+ if (true === o.tokenfails) {
77
+ return { status: 401, json: async () => ({ error: 'nope' }), headers: {} }
78
+ }
79
+ const body: any = {}
80
+ body[o.response || 'access_token'] = tokens[Math.min(issued, tokens.length - 1)]
81
+ issued++
82
+ return { status: 200, json: async () => body, headers: {} }
83
+ }
84
+
85
+ apicall++
86
+ const status = apiStatus[Math.min(apicall, apiStatus.length - 1)]
87
+ return { status, json: async () => ({ ok: status < 400 }), headers: {} }
88
+ }
89
+
90
+ return {
91
+ fetch,
92
+ calls,
93
+ // Only the calls that went to the API, in order.
94
+ api: () => calls.filter((c: any) => !c.url.endsWith('/' + (o.path || 'auth/token'))),
95
+ token: () => calls.filter((c: any) => c.url.endsWith('/' + (o.path || 'auth/token'))),
96
+ }
97
+ }
98
+
99
+
100
+ // Same rule as credentialIs, reading the header off a recorded call.
101
+ function authIs(call: any, token: string, secret?: string) {
102
+ credentialIs(null == call ? undefined : call.auth, token, secret)
46
103
  }
47
104
 
48
105
 
@@ -102,6 +159,13 @@ describe('secrets', { skip: NOAUTH }, () => {
102
159
  })
103
160
 
104
161
 
162
+ test('active: a resolved key is placed as prepareAuth places it, the secret kept', async () => {
163
+ process.env[ENVPREFIX + 'APIKEY'] = 'ENVKEY03'
164
+ const { fetchdef } = await prepared(Object.assign({ secret: 'PASS01' }, envchain()))
165
+ credentialIs(fetchdef.headers[CREDHEADER], 'ENVKEY03', 'PASS01')
166
+ })
167
+
168
+
105
169
  test('active: an explicitly EMPTY apikey also defers to the chain', async () => {
106
170
  process.env[ENVPREFIX + 'APIKEY'] = 'ENVKEY01'
107
171
  const { fetchdef } = await prepared(
@@ -379,56 +443,9 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
379
443
  })
380
444
 
381
445
 
382
- // A stub standing in for both endpoints the flow touches: the token
383
- // endpoint, and everything else.
384
- //
385
- // `apiStatus` is a SCRIPT — one status per API call, the last repeating —
386
- // so a case can say "401 then 200" without counting calls itself.
387
- function stubfetch(opts?: any) {
388
- const o = opts || {}
389
- const tokens: string[] = o.tokens || ['ACCESS01', 'ACCESS02', 'ACCESS03']
390
- const apiStatus: number[] = o.apiStatus || [200]
391
-
392
- const calls: any[] = []
393
- let issued = 0
394
- let apicall = -1
395
-
396
- const fetch = async (url: string, init: any) => {
397
- calls.push({ url, init, auth: (init.headers || {})[CREDHEADER] })
398
-
399
- if (url.endsWith('/' + (o.path || 'auth/token'))) {
400
- if (true === o.tokenfails) {
401
- return { status: 401, json: async () => ({ error: 'nope' }), headers: {} }
402
- }
403
- const body: any = {}
404
- body[o.response || 'access_token'] = tokens[Math.min(issued, tokens.length - 1)]
405
- issued++
406
- return { status: 200, json: async () => body, headers: {} }
407
- }
408
-
409
- apicall++
410
- const status = apiStatus[Math.min(apicall, apiStatus.length - 1)]
411
- return { status, json: async () => ({ ok: status < 400 }), headers: {} }
412
- }
413
-
414
- return {
415
- fetch,
416
- calls,
417
- // Only the calls that went to the API, in order.
418
- api: () => calls.filter((c: any) => !c.url.endsWith('/' + (o.path || 'auth/token'))),
419
- token: () => calls.filter((c: any) => c.url.endsWith('/' + (o.path || 'auth/token'))),
420
- }
421
- }
422
-
423
-
424
- // Same rule as credentialIs, reading the header off a recorded call.
425
- function authIs(call: any, token: string) {
426
- credentialIs(null == call ? undefined : call.auth, token)
427
- }
428
-
429
-
430
- function exchangeSdk(stub: any, extra?: any): any {
446
+ function exchangeSdk(stub: any, extra?: any, sdkopts?: any): any {
431
447
  return new (SDK as any)({
448
+ ...(sdkopts || {}),
432
449
  base: BASE,
433
450
  system: { fetch: stub.fetch },
434
451
  feature: {
@@ -528,6 +545,21 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
528
545
  })
529
546
 
530
547
 
548
+ test('a token bought on a refusal is placed as prepareAuth places it, the secret kept',
549
+ async () => {
550
+ process.env[ENVPREFIX + 'REFRESH_TOKEN'] = REFRESH
551
+
552
+ const stub = stubfetch({ apiStatus: [401, 200] })
553
+ const sdk = exchangeSdk(stub, {}, { secret: 'PASS01' })
554
+
555
+ await sdk.direct({ path: '/thing' })
556
+
557
+ assert.equal(stub.api().length, 2, 'expected the request to be retried')
558
+ authIs(stub.api()[0], 'ACCESS01', 'PASS01')
559
+ authIs(stub.api()[1], 'ACCESS02', 'PASS01')
560
+ })
561
+
562
+
531
563
  test('the retry happens once, not in a loop', async () => {
532
564
  process.env[ENVPREFIX + 'REFRESH_TOKEN'] = REFRESH
533
565
 
@@ -780,30 +812,31 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
780
812
  })
781
813
 
782
814
 
783
- // Real listeners and the platform fetch: a stub follows nothing.
784
- test('a redirect answering the exchange is a failure, and nothing follows it', async () => {
785
- const elsewhere: any[] = []
786
- const followed: string[] = []
787
- let answer: [number, string] = [307, '']
815
+ // Real listeners and the platform fetch: a stub follows nothing. The
816
+ // origin answers the exchange as `answer` says and records what else it gets.
817
+ async function withOrigins(run: (o: any) => Promise<void>) {
818
+ const o: any = { elsewhere: [], followed: [], exchanged: 0, answer: [307, ''] }
788
819
 
789
820
  const other = Http.createServer((req: any, res: any) => {
790
821
  let body = ''
791
822
  req.on('data', (chunk: any) => body += chunk)
792
823
  req.on('end', () => {
793
- elsewhere.push({ method: req.method, url: req.url, body })
824
+ o.elsewhere.push({ method: req.method, url: req.url, body })
794
825
  res.writeHead(200, { 'content-type': 'application/json', connection: 'close' })
795
826
  res.end('{"access_token":"ELSEWHERE01"}')
796
827
  })
797
828
  })
798
829
  const origin = Http.createServer((req: any, res: any) => {
799
- req.resume()
830
+ let body = ''
831
+ req.on('data', (chunk: any) => body += chunk)
800
832
  req.on('end', () => {
801
833
  if ('/api/auth/token' === req.url) {
802
- res.writeHead(answer[0], { location: answer[1], connection: 'close' })
834
+ o.exchanged++
835
+ res.writeHead(o.answer[0], { location: o.answer[1], connection: 'close' })
803
836
  res.end()
804
837
  return
805
838
  }
806
- followed.push(req.method + ' ' + req.url)
839
+ o.followed.push((req.method + ' ' + req.url + ' ' + body).trim())
807
840
  res.writeHead(200, { 'content-type': 'application/json', connection: 'close' })
808
841
  res.end('{"access_token":"MOVED01"}')
809
842
  })
@@ -811,33 +844,50 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
811
844
 
812
845
  const listen = (server: any) => new Promise<number>((resolve) =>
813
846
  server.listen(0, '127.0.0.1', () => resolve(server.address().port)))
814
- const base = 'http://127.0.0.1:' + (await listen(origin)) + '/api'
815
- const away = 'http://127.0.0.1:' + (await listen(other)) + '/api/auth/token'
847
+ o.origin = 'http://127.0.0.1:' + (await listen(origin))
848
+ o.base = o.origin + '/api'
849
+ o.away = 'http://127.0.0.1:' + (await listen(other)) + '/api/auth/token'
816
850
 
817
851
  try {
852
+ await run(o)
853
+ }
854
+ finally {
855
+ origin.close()
856
+ other.close()
857
+ }
858
+ }
859
+
860
+
861
+ function redirectSdk(o: any, exchange?: any): any {
862
+ return new (SDK as any)({
863
+ base: o.base,
864
+ feature: {
865
+ secrets: {
866
+ active: true,
867
+ name: 'refresh_token',
868
+ exchange: Object.assign({ active: true, refresh: REFRESH }, exchange || {}),
869
+ },
870
+ },
871
+ })
872
+ }
873
+
874
+
875
+ test('a redirect answering the exchange is a failure, and nothing follows it', async () => {
876
+ await withOrigins(async (o: any) => {
818
877
  const answers: [number, string][] =
819
- [[307, away], [308, away], [302, away], [307, '/api/moved/token']]
878
+ [[307, o.away], [308, o.away], [302, o.away], [307, '/api/moved/token']]
820
879
 
821
880
  const allowed: string[] = []
822
881
  let refused = 0
823
882
 
824
- for (answer of answers) {
825
- const sdk = new (SDK as any)({
826
- base,
827
- feature: {
828
- secrets: {
829
- active: true,
830
- name: 'refresh_token',
831
- exchange: { active: true, refresh: REFRESH },
832
- },
833
- },
834
- })
883
+ for (const answer of answers) {
884
+ o.answer = answer
835
885
 
836
- const res = await sdk.direct({ path: '/thing' })
886
+ const res = await redirectSdk(o).direct({ path: '/thing' })
837
887
  const message = String(null == res || null == res.err ? res : res.err.message)
838
888
 
839
889
  if (message.includes(
840
- 'token exchange failed: ' + answer[0] + ' from ' + base + '/auth/token')) {
890
+ 'token exchange failed: ' + answer[0] + ' from ' + o.base + '/auth/token')) {
841
891
  refused++
842
892
  }
843
893
  else {
@@ -846,17 +896,162 @@ describe('secrets exchange', { skip: NOAUTH }, () => {
846
896
  }
847
897
 
848
898
  console.log('secrets: exchange refused ' + refused + ' redirect(s), ' +
849
- (elsewhere.length + followed.length) + ' followed')
899
+ (o.elsewhere.length + o.followed.length) + ' followed')
850
900
 
851
- assert.deepEqual(elsewhere, [], 'the exchange reached another origin')
852
- assert.deepEqual(followed, [], 'a request followed a refused exchange')
901
+ assert.deepEqual(o.elsewhere, [], 'the exchange reached another origin')
902
+ assert.deepEqual(o.followed, [], 'a request followed a refused exchange')
853
903
  assert.deepEqual(allowed, [], 'a redirect was not a failed exchange')
854
904
  assert.equal(refused, answers.length)
855
- }
856
- finally {
857
- origin.close()
858
- other.close()
859
- }
905
+ })
906
+ })
907
+
908
+
909
+ test('with the option, a redirect on the exchange\'s own origin is followed, and no other', async () => {
910
+ await withOrigins(async (o: any) => {
911
+ const sent = ' {"refresh_token":"' + REFRESH + '"}'
912
+ // Each case: the status, the Location, and the path the exchange is
913
+ // asked again at, or null where the redirect is refused.
914
+ const cases: [number, string, string | null][] = [
915
+ [307, '/api/moved/token', '/api/moved/token'],
916
+ [308, o.origin + '/api/moved/token', '/api/moved/token'],
917
+ // A Location of a query alone keeps the path that answered.
918
+ [307, '?step=2', '/api/auth/token?step=2'],
919
+ [307, o.away, null],
920
+ [302, '/api/moved/token', null],
921
+ [307, '/api/auth/token', null],
922
+ ]
923
+
924
+ const wrong: string[] = []
925
+ let followed = 0
926
+ let refused = 0
927
+ let looped = 0
928
+
929
+ for (const [status, location, path] of cases) {
930
+ o.answer = [status, location]
931
+ o.followed.length = 0
932
+ o.exchanged = 0
933
+
934
+ const res = await redirectSdk(o, { redirect: 'same-origin' }).direct({ path: '/thing' })
935
+ const message = String(null == res || null == res.err ? res : res.err.message)
936
+ const seen = status + ' to ' + location + ': ' + message + ' ' + JSON.stringify(o.followed)
937
+
938
+ if (null != path) {
939
+ if (true === res.ok && 'POST ' + path + sent === o.followed[0] &&
940
+ 2 === o.followed.length) followed++
941
+ else wrong.push(seen)
942
+ }
943
+ else if (0 === o.followed.length && message.includes(
944
+ 'token exchange failed: ' + status + ' from ' + o.base + '/auth/token')) {
945
+ refused++
946
+ }
947
+ else {
948
+ wrong.push(seen)
949
+ }
950
+
951
+ if ('/api/auth/token' === location) looped = o.exchanged
952
+ }
953
+
954
+ console.log('secrets: exchange opt-in followed ' + followed +
955
+ ' redirect(s) on its own origin, refused ' + refused + ', ' +
956
+ o.elsewhere.length + ' elsewhere')
957
+
958
+ assert.deepEqual(o.elsewhere, [], 'the exchange reached another origin')
959
+ assert.deepEqual(wrong, [])
960
+ assert.equal(followed, 3)
961
+ assert.equal(refused, 3)
962
+ // A redirect to itself stops at the hop limit.
963
+ assert.equal(looped, 6)
964
+ })
965
+ })
966
+
967
+ })
968
+
969
+
970
+ // Off by the config where it declares no auth, else by `auth: null`: either
971
+ // way the feature places no credential, buys no token and retries nothing.
972
+ const AUTHOFF: { by: string, opts: any } =
973
+ NOAUTH ? { by: 'config', opts: {} } : { by: 'option', opts: { auth: null } }
974
+
975
+
976
+ describe('secrets with auth off', () => {
977
+
978
+ const BASE = 'http://authoff.test/api'
979
+ const SECRETS = ['OFFKEY01', 'OFFKEY02', 'OFFREFRESH01', 'ACCESS01']
980
+ const sent: any[] = []
981
+ let bought = 0
982
+
983
+
984
+ beforeEach(() => {
985
+ delete process.env[ENVPREFIX + 'APIKEY']
986
+ delete process.env[ENVPREFIX + 'REFRESH_TOKEN']
987
+ })
988
+
989
+
990
+ after(() => {
991
+ console.log('secrets: auth off by ' + AUTHOFF.by + ': ' + sent.length + ' request(s), ' +
992
+ sent.filter(carries).length + ' credential(s), ' + bought + ' token(s)')
993
+ })
994
+
995
+
996
+ // The header prepareAuth writes, or any header holding one of the secrets.
997
+ function carries(call: any): boolean {
998
+ const headers = call.init.headers || {}
999
+ return undefined !== headers[CREDHEADER] || Object.values(headers)
1000
+ .some((v: any) => SECRETS.some((secret: string) => String(v).includes(secret)))
1001
+ }
1002
+
1003
+
1004
+ async function offsdk(stub: any, sdkopts: any, fopts: any): Promise<any> {
1005
+ const sdk = new (SDK as any)({
1006
+ ...sdkopts,
1007
+ ...AUTHOFF.opts,
1008
+ base: BASE,
1009
+ system: { fetch: stub.fetch },
1010
+ feature: {
1011
+ secrets: {
1012
+ active: true,
1013
+ providers: [{ kind: 'env', prefix: ENVPREFIX }],
1014
+ ...fopts,
1015
+ },
1016
+ },
1017
+ })
1018
+ await sdk.direct({ path: '/thing' })
1019
+ sent.push(...stub.api())
1020
+ bought += stub.token().length
1021
+ return sdk
1022
+ }
1023
+
1024
+
1025
+ test('a key the chain resolves is not placed', async () => {
1026
+ process.env[ENVPREFIX + 'APIKEY'] = 'OFFKEY01'
1027
+
1028
+ const stub = stubfetch()
1029
+ const sdk = await offsdk(stub, {}, {})
1030
+
1031
+ assert.equal(stub.api().length, 1)
1032
+ assert.ok(!carries(stub.api()[0]), 'a credential was sent with auth off')
1033
+ assert.equal(sdk.options().apikey, '', 'the resolved key was placed where prepareAuth reads it')
1034
+ })
1035
+
1036
+
1037
+ test('an explicit apikey is not placed', async () => {
1038
+ const stub = stubfetch()
1039
+ await offsdk(stub, { apikey: 'OFFKEY02' }, {})
1040
+
1041
+ assert.equal(stub.api().length, 1)
1042
+ assert.ok(!carries(stub.api()[0]), 'a credential was sent with auth off')
1043
+ })
1044
+
1045
+
1046
+ test('a refusal buys no token and is not retried', async () => {
1047
+ process.env[ENVPREFIX + 'REFRESH_TOKEN'] = 'OFFREFRESH01'
1048
+
1049
+ const stub = stubfetch({ apiStatus: [401] })
1050
+ await offsdk(stub, {}, { name: 'refresh_token', exchange: { active: true } })
1051
+
1052
+ assert.equal(stub.token().length, 0, 'a token was bought with auth off')
1053
+ assert.equal(stub.api().length, 1, 'a refusal was retried with auth off')
1054
+ assert.ok(!carries(stub.api()[0]), 'a credential was sent with auth off')
860
1055
  })
861
1056
 
862
1057
  })
@@ -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: any[] = []
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
 
@@ -1197,7 +1197,7 @@ pub fn make_spec_util(ctx: *Context) E!*Spec {
1197
1197
  if (std.mem.eql(u8, kind, "graphql")) {
1198
1198
  // GraphQL addresses one endpoint: no path parts, no query string,
1199
1199
  // and the body carries the operation. prepare_body is skipped
1200
- // deliberately — it only emits a body for data-input ops, whereas
1200
+ // deliberately — it builds only a REST body, whereas
1201
1201
  // every GraphQL op posts one, including load/list/remove.
1202
1202
  spec.body = graphql_body_util(ctx);
1203
1203
  spec.path = "";
@@ -1779,9 +1779,38 @@ pub fn prepare_body_util(ctx: *Context) Value {
1779
1779
  if (is_raw_request(ctx.point)) return raw_body(ctx.reqdata);
1780
1780
  return transform_request_util(ctx);
1781
1781
  }
1782
+ const fields = h.getp(ctx.point, "fields");
1783
+ if (fields == .array and fields.array.data.items.len != 0) return match_body(ctx, fields);
1782
1784
  return h.vnull();
1783
1785
  }
1784
1786
 
1787
+ // A list, load or remove routed to a method with a body sends the match
1788
+ // fields that body declares, never a path parameter, and never a header,
1789
+ // cookie or query argument the body does not also declare as a field.
1790
+ fn match_body(ctx: *Context, fields: Value) Value {
1791
+ const body = h.omap();
1792
+ for (fields.array.data.items) |f| {
1793
+ if (f != .string or std.mem.eql(u8, f.string, "$action") or body_routed(ctx.point, f.string)) continue;
1794
+ const val = h.getp(ctx.reqmatch, f.string);
1795
+ if (!h.is_noval(val)) h.setp(body, f.string, val);
1796
+ }
1797
+ return body;
1798
+ }
1799
+
1800
+ fn body_routed(point: Value, key: []const u8) bool {
1801
+ if (listed(h.getp(point, "params"), key) or names_key(h.getpath(&.{ "args", "params" }, point), key)) return true;
1802
+ for ([_][]const u8{ "header", "cookie", "query" }) |kind| {
1803
+ const defs: Value = h.getpath(&.{ "args", kind }, point);
1804
+ if (defs != .array) continue;
1805
+ for (defs.array.data.items) |ad| {
1806
+ const n = h.getp(ad, "name");
1807
+ const f = h.getp(ad, "field");
1808
+ if (n == .string and std.mem.eql(u8, n.string, key) and !(f == .bool and f.bool)) return true;
1809
+ }
1810
+ }
1811
+ return false;
1812
+ }
1813
+
1785
1814
  pub fn prepare_params_util(ctx: *Context) Value {
1786
1815
  const point = ctx.point;
1787
1816
  const args = h.to_map(h.getp(point, "args"));
@@ -1828,6 +1857,15 @@ fn names_key(defs: Value, key: []const u8) bool {
1828
1857
  return false;
1829
1858
  }
1830
1859
 
1860
+ // Whether a list of names holds this key.
1861
+ fn listed(names: Value, key: []const u8) bool {
1862
+ if (names != .array) return false;
1863
+ for (names.array.data.items) |v| {
1864
+ if (v == .string and std.mem.eql(u8, v.string, key)) return true;
1865
+ }
1866
+ return false;
1867
+ }
1868
+
1831
1869
  pub fn prepare_query_util(ctx: *Context) Value {
1832
1870
  const point = ctx.point;
1833
1871
  const reqmatch: Value = switch (ctx.reqmatch) {
@@ -1842,9 +1880,12 @@ pub fn prepare_query_util(ctx: *Context) Value {
1842
1880
  // as args.params, which prepare_params reads; params is the older list.
1843
1881
  const aparams: Value = h.getpath(&.{ "args", "params" }, point);
1844
1882
  // A header or cookie parameter travels in the headers, which prepare_headers
1845
- // fills, unless a query parameter shares its name: then both are sent.
1883
+ // fills, and a field the point's body declares travels in the body, which
1884
+ // prepare_body fills, unless a query parameter shares its name: then both
1885
+ // are sent.
1846
1886
  const aheader: Value = h.getpath(&.{ "args", "header" }, point);
1847
1887
  const acookie: Value = h.getpath(&.{ "args", "cookie" }, point);
1888
+ const fields: Value = h.getp(point, "fields");
1848
1889
  // A query parameter travels under the name the definition gives it, its
1849
1890
  // orig, which the model may have renamed for the caller.
1850
1891
  const aquery: Value = h.getpath(&.{ "args", "query" }, point);
@@ -1865,7 +1906,8 @@ pub fn prepare_query_util(ctx: *Context) Value {
1865
1906
  }
1866
1907
  }
1867
1908
  if (!contained) contained = names_key(aparams, key) or
1868
- ((names_key(aheader, key) or names_key(acookie, key)) and !names_key(aquery, key));
1909
+ ((names_key(aheader, key) or names_key(acookie, key) or listed(fields, key)) and
1910
+ !names_key(aquery, key));
1869
1911
  var wire: []const u8 = key;
1870
1912
  if (aquery == .array) {
1871
1913
  for (aquery.array.data.items) |qd| {
@@ -1886,7 +1928,8 @@ pub fn prepare_query_util(ctx: *Context) Value {
1886
1928
  // A create or update passes its query arguments in its data.
1887
1929
  for (call_args(ctx, "query")) |arg| {
1888
1930
  var contained = h.is_noval(arg.val) or names_key(aparams, arg.name) or
1889
- ((names_key(aheader, arg.name) or names_key(acookie, arg.name)) and !names_key(aquery, arg.name));
1931
+ ((names_key(aheader, arg.name) or names_key(acookie, arg.name) or listed(fields, arg.name)) and
1932
+ !names_key(aquery, arg.name));
1890
1933
  if (params == .array) {
1891
1934
  for (params.array.data.items) |v| {
1892
1935
  if (v == .string and std.mem.eql(u8, v.string, arg.name)) contained = true;
@@ -13,6 +13,7 @@ const h = @import("../core/helpers.zig");
13
13
  const err = @import("../core/error.zig");
14
14
  const types = @import("../core/types.zig");
15
15
  const sup = @import("support.zig");
16
+ const media = @import("../core/utility.zig");
16
17
 
17
18
  const Value = h.Value;
18
19
  const Context = types.Context;
@@ -146,7 +147,7 @@ fn through(track: *CacheTrack, options: Value, ctx: *Context, url: []const u8, f
146
147
  }
147
148
 
148
149
  if (inner.invoke(ctx, url, fetchdef)) |res| {
149
- if (storable(res)) {
150
+ if (storable(res) and json(res)) {
150
151
  const snap = h.A().create(CacheSnapshot) catch unreachable;
151
152
  snap.* = snapshot(res);
152
153
  const ttl = sup.fopt_int(options, "ttl", 5000);
@@ -171,6 +172,19 @@ fn storable(res: Value) bool {
171
172
  return status >= 200 and status < 300;
172
173
  }
173
174
 
175
+ // A body that declares another type than JSON passes by, unread.
176
+ fn json(res: Value) bool {
177
+ const headers = h.getp(res, "headers");
178
+ if (headers != .object) return true;
179
+ var it = headers.object.iterator();
180
+ while (it.next()) |kv| {
181
+ if (!std.ascii.eqlIgnoreCase(kv.key_ptr.*, "content-type") or kv.value_ptr.* != .string) continue;
182
+ const declared = kv.value_ptr.*.string;
183
+ return 0 == std.mem.trim(u8, declared, " \t").len or media.is_json_media(declared);
184
+ }
185
+ return true;
186
+ }
187
+
174
188
  fn snapshot(res: Value) CacheSnapshot {
175
189
  const headers = h.omap();
176
190
  const rh = h.getp(res, "headers");