@voxgig/sdkgen 4.48.1 → 4.49.1

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 (119) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/package.json +1 -1
  3. package/project/.sdk/model/feature/secrets.aontu +3 -0
  4. package/project/.sdk/src/cmp/c/Config_c.ts +6 -1
  5. package/project/.sdk/src/cmp/c/fragment/Entity.fragment.c +0 -10
  6. package/project/.sdk/src/cmp/clojure/fragment/Entity.fragment.clj +0 -9
  7. package/project/.sdk/src/cmp/csharp/fragment/EntityBase.fragment.cs +0 -11
  8. package/project/.sdk/src/cmp/go/fragment/Entity.fragment.go +0 -7
  9. package/project/.sdk/src/cmp/java/fragment/EntityBase.fragment.java +0 -10
  10. package/project/.sdk/src/cmp/js/fragment/EntityBase.fragment.js +2 -11
  11. package/project/.sdk/src/cmp/kotlin/fragment/EntityBase.fragment.kt +0 -10
  12. package/project/.sdk/src/cmp/lua/fragment/Entity.fragment.lua +0 -11
  13. package/project/.sdk/src/cmp/ocaml/Entity_ocaml.ts +2 -10
  14. package/project/.sdk/src/cmp/perl/fragment/Entity.fragment.pm +0 -13
  15. package/project/.sdk/src/cmp/php/fragment/Entity.fragment.php +0 -10
  16. package/project/.sdk/src/cmp/py/fragment/Entity.fragment.py +0 -10
  17. package/project/.sdk/src/cmp/rb/fragment/Entity.fragment.rb +0 -10
  18. package/project/.sdk/src/cmp/rust/fragment/Entity.fragment.rs +0 -12
  19. package/project/.sdk/src/cmp/scala/fragment/EntityBase.fragment.scala +0 -10
  20. package/project/.sdk/src/cmp/swift/fragment/EntityBase.fragment.swift +0 -9
  21. package/project/.sdk/src/cmp/ts/fragment/EntityBase.fragment.ts +0 -7
  22. package/project/.sdk/src/cmp/zig/fragment/Entity.fragment.zig +1 -14
  23. package/project/.sdk/tm/c/feature/cache.c +16 -1
  24. package/project/.sdk/tm/c/feature/secrets.c +149 -18
  25. package/project/.sdk/tm/c/tests/feature/secrets/secrets_test.c +319 -70
  26. package/project/.sdk/tm/c/tests/feature_test.c +31 -0
  27. package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +74 -29
  28. package/project/.sdk/tm/clojure/src/sdk/core.clj +55 -6
  29. package/project/.sdk/tm/clojure/src/sdk/features.clj +16 -2
  30. package/project/.sdk/tm/clojure/test/sdk/test/feature/secrets.clj +191 -48
  31. package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +16 -0
  32. package/project/.sdk/tm/cpp/core/types.hpp +1 -11
  33. package/project/.sdk/tm/cpp/feature/cache.hpp +14 -1
  34. package/project/.sdk/tm/cpp/feature/secrets.hpp +133 -18
  35. package/project/.sdk/tm/cpp/test/feature/secrets/secrets_test.cpp +175 -8
  36. package/project/.sdk/tm/cpp/test/feature_test.cpp +27 -0
  37. package/project/.sdk/tm/csharp/core/Control.cs +0 -5
  38. package/project/.sdk/tm/csharp/feature/CacheFeature.cs +16 -0
  39. package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +77 -38
  40. package/project/.sdk/tm/csharp/test/FeatureTest.cs +29 -0
  41. package/project/.sdk/tm/csharp/test/feature/secrets/SecretsFeatureTest.cs +315 -68
  42. package/project/.sdk/tm/elixir/lib/projectname/entity_base.ex +0 -13
  43. package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +32 -10
  44. package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +78 -22
  45. package/project/.sdk/tm/elixir/test/feature/secrets/secrets_test.exs +178 -19
  46. package/project/.sdk/tm/elixir/test/feature_test.exs +20 -0
  47. package/project/.sdk/tm/go/feature/cache_feature.go +14 -1
  48. package/project/.sdk/tm/go/feature/secrets_feature.go +110 -15
  49. package/project/.sdk/tm/go/test/feature/secrets/secrets_feature_test.go +373 -87
  50. package/project/.sdk/tm/go/test/feature_test.go +32 -0
  51. package/project/.sdk/tm/go/utility/media.go +2 -2
  52. package/project/.sdk/tm/java/core/Control.java +0 -5
  53. package/project/.sdk/tm/java/feature/CacheFeature.java +12 -0
  54. package/project/.sdk/tm/java/feature/SecretsFeature.java +104 -34
  55. package/project/.sdk/tm/java/test/FeatureTest.java +25 -0
  56. package/project/.sdk/tm/java/test/PrimaryUtilityTest.java +39 -0
  57. package/project/.sdk/tm/java/test/feature/secrets/SecretsFeatureTest.java +267 -53
  58. package/project/.sdk/tm/java/utility/Fetcher.java +18 -2
  59. package/project/.sdk/tm/java/utility/Media.java +2 -2
  60. package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +13 -0
  61. package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +79 -29
  62. package/project/.sdk/tm/js/test/feature/secrets/Secrets.test.js +282 -93
  63. package/project/.sdk/tm/js/test/feature.test.js +17 -0
  64. package/project/.sdk/tm/kotlin/core/Control.kt +0 -5
  65. package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +12 -0
  66. package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +84 -24
  67. package/project/.sdk/tm/kotlin/test/FeatureTest.kt +24 -0
  68. package/project/.sdk/tm/kotlin/test/PrimaryUtilityTest.kt +35 -0
  69. package/project/.sdk/tm/kotlin/test/feature/secrets/SecretsTest.kt +236 -54
  70. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +13 -1
  71. package/project/.sdk/tm/lua/feature/cache_feature.lua +20 -0
  72. package/project/.sdk/tm/lua/feature/secrets_feature.lua +92 -15
  73. package/project/.sdk/tm/lua/test/feature/secrets/secrets_feature_test.lua +208 -43
  74. package/project/.sdk/tm/lua/test/feature_test.lua +18 -0
  75. package/project/.sdk/tm/lua/utility/clean.lua +1 -0
  76. package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +100 -14
  77. package/project/.sdk/tm/ocaml/sdk_features.ml +7 -1
  78. package/project/.sdk/tm/ocaml/test/feature/secrets/t_secrets.ml +143 -7
  79. package/project/.sdk/tm/ocaml/test/t_feature.ml +15 -0
  80. package/project/.sdk/tm/perl/feature/cache_feature.pm +13 -0
  81. package/project/.sdk/tm/perl/feature/secrets_feature.pm +87 -32
  82. package/project/.sdk/tm/perl/t/feature/secrets/secrets.t +177 -24
  83. package/project/.sdk/tm/perl/t/feature.t +24 -0
  84. package/project/.sdk/tm/php/feature/CacheFeature.php +13 -0
  85. package/project/.sdk/tm/php/feature/SecretsFeature.php +79 -23
  86. package/project/.sdk/tm/php/test/FeatureTest.php +23 -0
  87. package/project/.sdk/tm/php/test/feature/secrets/SecretsTest.php +258 -48
  88. package/project/.sdk/tm/py/pkg/feature/cache_feature.py +7 -1
  89. package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +90 -29
  90. package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +313 -80
  91. package/project/.sdk/tm/py/test/test_feature.py +18 -0
  92. package/project/.sdk/tm/rb/feature/cache_feature.rb +12 -0
  93. package/project/.sdk/tm/rb/feature/secrets_feature.rb +68 -21
  94. package/project/.sdk/tm/rb/test/feature/secrets/secrets_feature_test.rb +214 -43
  95. package/project/.sdk/tm/rb/test/feature_test.rb +20 -0
  96. package/project/.sdk/tm/rust/feature/cache.rs +15 -1
  97. package/project/.sdk/tm/rust/feature/secrets.rs +103 -23
  98. package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +357 -67
  99. package/project/.sdk/tm/rust/tests/feature_test.rs +41 -0
  100. package/project/.sdk/tm/scala/core/Control.scala +0 -5
  101. package/project/.sdk/tm/scala/feature/CacheFeature.scala +12 -0
  102. package/project/.sdk/tm/scala/feature/SecretsFeature.scala +88 -22
  103. package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +61 -1
  104. package/project/.sdk/tm/scala/sdktest/feature/secrets/SecretsTestMain.scala +230 -73
  105. package/project/.sdk/tm/scala/utility/Fetcher.scala +13 -1
  106. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Control.swift +0 -5
  107. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +11 -0
  108. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +76 -16
  109. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +21 -0
  110. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/feature/secrets/SecretsFeatureTest.swift +257 -73
  111. package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +13 -0
  112. package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +77 -17
  113. package/project/.sdk/tm/ts/test/feature/secrets/Secrets.test.ts +282 -87
  114. package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
  115. package/project/.sdk/tm/zig/feature/cache.zig +15 -1
  116. package/project/.sdk/tm/zig/feature/secrets.zig +109 -32
  117. package/project/.sdk/tm/zig/test/feature/secrets/secrets_test.zig +232 -9
  118. package/project/.sdk/tm/zig/test/feature_test.zig +33 -0
  119. package/project/sdkgen-package.json +1 -1
@@ -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
 
@@ -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");