@onkernel/sdk 0.149.0 → 0.151.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 (191) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/client.d.mts +29 -4
  3. package/client.d.mts.map +1 -1
  4. package/client.d.ts +29 -4
  5. package/client.d.ts.map +1 -1
  6. package/client.js +32 -3
  7. package/client.js.map +1 -1
  8. package/client.mjs +32 -3
  9. package/client.mjs.map +1 -1
  10. package/lib/credentials/client-auth.d.mts +88 -0
  11. package/lib/credentials/client-auth.d.mts.map +1 -0
  12. package/lib/credentials/client-auth.d.ts +88 -0
  13. package/lib/credentials/client-auth.d.ts.map +1 -0
  14. package/lib/credentials/client-auth.js +166 -0
  15. package/lib/credentials/client-auth.js.map +1 -0
  16. package/lib/credentials/client-auth.mjs +162 -0
  17. package/lib/credentials/client-auth.mjs.map +1 -0
  18. package/lib/credentials/credential-chain.d.mts +20 -0
  19. package/lib/credentials/credential-chain.d.mts.map +1 -0
  20. package/lib/credentials/credential-chain.d.ts +20 -0
  21. package/lib/credentials/credential-chain.d.ts.map +1 -0
  22. package/lib/credentials/credential-chain.js +81 -0
  23. package/lib/credentials/credential-chain.js.map +1 -0
  24. package/lib/credentials/credential-chain.mjs +77 -0
  25. package/lib/credentials/credential-chain.mjs.map +1 -0
  26. package/lib/credentials/identity-token.d.mts +59 -0
  27. package/lib/credentials/identity-token.d.mts.map +1 -0
  28. package/lib/credentials/identity-token.d.ts +59 -0
  29. package/lib/credentials/identity-token.d.ts.map +1 -0
  30. package/lib/credentials/identity-token.js +243 -0
  31. package/lib/credentials/identity-token.js.map +1 -0
  32. package/lib/credentials/identity-token.mjs +202 -0
  33. package/lib/credentials/identity-token.mjs.map +1 -0
  34. package/lib/credentials/oidc-federation.d.mts +65 -0
  35. package/lib/credentials/oidc-federation.d.mts.map +1 -0
  36. package/lib/credentials/oidc-federation.d.ts +65 -0
  37. package/lib/credentials/oidc-federation.d.ts.map +1 -0
  38. package/lib/credentials/oidc-federation.js +200 -0
  39. package/lib/credentials/oidc-federation.js.map +1 -0
  40. package/lib/credentials/oidc-federation.mjs +196 -0
  41. package/lib/credentials/oidc-federation.mjs.map +1 -0
  42. package/lib/credentials/token-cache.d.mts +64 -0
  43. package/lib/credentials/token-cache.d.mts.map +1 -0
  44. package/lib/credentials/token-cache.d.ts +64 -0
  45. package/lib/credentials/token-cache.d.ts.map +1 -0
  46. package/lib/credentials/token-cache.js +147 -0
  47. package/lib/credentials/token-cache.js.map +1 -0
  48. package/lib/credentials/token-cache.mjs +143 -0
  49. package/lib/credentials/token-cache.mjs.map +1 -0
  50. package/lib/credentials/types.d.mts +126 -0
  51. package/lib/credentials/types.d.mts.map +1 -0
  52. package/lib/credentials/types.d.ts +126 -0
  53. package/lib/credentials/types.d.ts.map +1 -0
  54. package/lib/credentials/types.js +182 -0
  55. package/lib/credentials/types.js.map +1 -0
  56. package/lib/credentials/types.mjs +169 -0
  57. package/lib/credentials/types.mjs.map +1 -0
  58. package/lib/credentials.d.mts +6 -0
  59. package/lib/credentials.d.mts.map +1 -0
  60. package/lib/credentials.d.ts +6 -0
  61. package/lib/credentials.d.ts.map +1 -0
  62. package/lib/credentials.js +19 -0
  63. package/lib/credentials.js.map +1 -0
  64. package/lib/credentials.mjs +6 -0
  65. package/lib/credentials.mjs.map +1 -0
  66. package/package.json +9 -1
  67. package/resources/api-keys.d.mts +192 -16
  68. package/resources/api-keys.d.mts.map +1 -1
  69. package/resources/api-keys.d.ts +192 -16
  70. package/resources/api-keys.d.ts.map +1 -1
  71. package/resources/api-keys.js +19 -7
  72. package/resources/api-keys.js.map +1 -1
  73. package/resources/api-keys.mjs +19 -7
  74. package/resources/api-keys.mjs.map +1 -1
  75. package/resources/auth/auth.d.mts +2 -2
  76. package/resources/auth/auth.d.mts.map +1 -1
  77. package/resources/auth/auth.d.ts +2 -2
  78. package/resources/auth/auth.d.ts.map +1 -1
  79. package/resources/auth/auth.js.map +1 -1
  80. package/resources/auth/auth.mjs.map +1 -1
  81. package/resources/auth/connections.d.mts +16 -2
  82. package/resources/auth/connections.d.mts.map +1 -1
  83. package/resources/auth/connections.d.ts +16 -2
  84. package/resources/auth/connections.d.ts.map +1 -1
  85. package/resources/auth/context.d.mts +69 -3
  86. package/resources/auth/context.d.mts.map +1 -1
  87. package/resources/auth/context.d.ts +69 -3
  88. package/resources/auth/context.d.ts.map +1 -1
  89. package/resources/auth/index.d.mts +1 -1
  90. package/resources/auth/index.d.mts.map +1 -1
  91. package/resources/auth/index.d.ts +1 -1
  92. package/resources/auth/index.d.ts.map +1 -1
  93. package/resources/auth/index.js.map +1 -1
  94. package/resources/auth/index.mjs.map +1 -1
  95. package/resources/browser-pools.d.mts +41 -6
  96. package/resources/browser-pools.d.mts.map +1 -1
  97. package/resources/browser-pools.d.ts +41 -6
  98. package/resources/browser-pools.d.ts.map +1 -1
  99. package/resources/browsers/browsers.d.mts +105 -45
  100. package/resources/browsers/browsers.d.mts.map +1 -1
  101. package/resources/browsers/browsers.d.ts +105 -45
  102. package/resources/browsers/browsers.d.ts.map +1 -1
  103. package/resources/browsers/browsers.js.map +1 -1
  104. package/resources/browsers/browsers.mjs.map +1 -1
  105. package/resources/browsers/index.d.mts +1 -1
  106. package/resources/browsers/index.d.mts.map +1 -1
  107. package/resources/browsers/index.d.ts +1 -1
  108. package/resources/browsers/index.d.ts.map +1 -1
  109. package/resources/browsers/index.js.map +1 -1
  110. package/resources/browsers/index.mjs.map +1 -1
  111. package/resources/browsers/telemetry.d.mts +4 -4
  112. package/resources/browsers/telemetry.d.mts.map +1 -1
  113. package/resources/browsers/telemetry.d.ts +4 -4
  114. package/resources/browsers/telemetry.d.ts.map +1 -1
  115. package/resources/index.d.mts +6 -2
  116. package/resources/index.d.mts.map +1 -1
  117. package/resources/index.d.ts +6 -2
  118. package/resources/index.d.ts.map +1 -1
  119. package/resources/index.js +9 -1
  120. package/resources/index.js.map +1 -1
  121. package/resources/index.mjs +4 -0
  122. package/resources/index.mjs.map +1 -1
  123. package/resources/invocations.d.mts +13 -1
  124. package/resources/invocations.d.mts.map +1 -1
  125. package/resources/invocations.d.ts +13 -1
  126. package/resources/invocations.d.ts.map +1 -1
  127. package/resources/service-accounts.d.mts +73 -0
  128. package/resources/service-accounts.d.mts.map +1 -0
  129. package/resources/service-accounts.d.ts +73 -0
  130. package/resources/service-accounts.d.ts.map +1 -0
  131. package/resources/service-accounts.js +50 -0
  132. package/resources/service-accounts.js.map +1 -0
  133. package/resources/service-accounts.mjs +46 -0
  134. package/resources/service-accounts.mjs.map +1 -0
  135. package/resources/vaults/items.d.mts +68 -2
  136. package/resources/vaults/items.d.mts.map +1 -1
  137. package/resources/vaults/items.d.ts +68 -2
  138. package/resources/vaults/items.d.ts.map +1 -1
  139. package/resources/workload-identity-authentication-events.d.mts +36 -0
  140. package/resources/workload-identity-authentication-events.d.mts.map +1 -0
  141. package/resources/workload-identity-authentication-events.d.ts +36 -0
  142. package/resources/workload-identity-authentication-events.d.ts.map +1 -0
  143. package/resources/workload-identity-authentication-events.js +16 -0
  144. package/resources/workload-identity-authentication-events.js.map +1 -0
  145. package/resources/workload-identity-authentication-events.mjs +12 -0
  146. package/resources/workload-identity-authentication-events.mjs.map +1 -0
  147. package/resources/workload-identity-federation-rules.d.mts +194 -0
  148. package/resources/workload-identity-federation-rules.d.mts.map +1 -0
  149. package/resources/workload-identity-federation-rules.d.ts +194 -0
  150. package/resources/workload-identity-federation-rules.d.ts.map +1 -0
  151. package/resources/workload-identity-federation-rules.js +47 -0
  152. package/resources/workload-identity-federation-rules.js.map +1 -0
  153. package/resources/workload-identity-federation-rules.mjs +43 -0
  154. package/resources/workload-identity-federation-rules.mjs.map +1 -0
  155. package/resources/workload-identity-issuers.d.mts +169 -0
  156. package/resources/workload-identity-issuers.d.mts.map +1 -0
  157. package/resources/workload-identity-issuers.d.ts +169 -0
  158. package/resources/workload-identity-issuers.d.ts.map +1 -0
  159. package/resources/workload-identity-issuers.js +54 -0
  160. package/resources/workload-identity-issuers.js.map +1 -0
  161. package/resources/workload-identity-issuers.mjs +50 -0
  162. package/resources/workload-identity-issuers.mjs.map +1 -0
  163. package/src/client.ts +134 -1
  164. package/src/lib/credentials/client-auth.ts +194 -0
  165. package/src/lib/credentials/credential-chain.ts +95 -0
  166. package/src/lib/credentials/identity-token.ts +284 -0
  167. package/src/lib/credentials/oidc-federation.ts +304 -0
  168. package/src/lib/credentials/token-cache.ts +172 -0
  169. package/src/lib/credentials/types.ts +267 -0
  170. package/src/lib/credentials.ts +30 -0
  171. package/src/resources/api-keys.ts +217 -15
  172. package/src/resources/auth/auth.ts +6 -2
  173. package/src/resources/auth/connections.ts +17 -2
  174. package/src/resources/auth/context.ts +81 -3
  175. package/src/resources/auth/index.ts +1 -1
  176. package/src/resources/browser-pools.ts +46 -6
  177. package/src/resources/browsers/browsers.ts +115 -44
  178. package/src/resources/browsers/index.ts +1 -0
  179. package/src/resources/browsers/telemetry.ts +4 -3
  180. package/src/resources/index.ts +42 -0
  181. package/src/resources/invocations.ts +15 -1
  182. package/src/resources/service-accounts.ts +128 -0
  183. package/src/resources/vaults/items.ts +80 -2
  184. package/src/resources/workload-identity-authentication-events.ts +72 -0
  185. package/src/resources/workload-identity-federation-rules.ts +314 -0
  186. package/src/resources/workload-identity-issuers.ts +273 -0
  187. package/src/version.ts +1 -1
  188. package/version.d.mts +1 -1
  189. package/version.d.ts +1 -1
  190. package/version.js +1 -1
  191. package/version.mjs +1 -1
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TokenCache = void 0;
4
+ const types_1 = require("./types.js");
5
+ /**
6
+ * Wraps an {@link AccessTokenProvider} with two-tier proactive refresh and
7
+ * concurrent deduplication.
8
+ *
9
+ * The refresh thresholds scale with the token's lifetime (the time it had
10
+ * left when it was cached), so a token that lives for a few seconds is not
11
+ * refreshed on every call:
12
+ *
13
+ * - advisory: min(120s, 40% of the lifetime) remaining;
14
+ * - mandatory: min(30s, 10% of the lifetime) remaining.
15
+ *
16
+ * On each {@link getToken} call:
17
+ *
18
+ * - No cached token: call the provider, cache the result and return it.
19
+ * - Cached with `expiresAt == null`: return it forever.
20
+ * - More than the advisory threshold remaining: return the cached token.
21
+ * - Between the two thresholds: return the cached token and refresh in the
22
+ * background. A failure is reported to `onAdvisoryRefreshError` and the
23
+ * cached token keeps being served.
24
+ * - Less than the mandatory threshold remaining, or expired: wait for a
25
+ * refresh, and throw if it fails.
26
+ *
27
+ * Concurrent callers share one provider call.
28
+ */
29
+ class TokenCache {
30
+ constructor(provider, onAdvisoryRefreshError) {
31
+ this.cached = null;
32
+ this.cachedLifetime = 0;
33
+ this.pendingRefresh = null;
34
+ this.pendingIsForced = false;
35
+ this.queuedForcedRefresh = null;
36
+ this.nextForce = false;
37
+ this.invalidations = 0;
38
+ this.lastAdvisoryError = -Infinity;
39
+ this.provider = provider;
40
+ this.onAdvisoryRefreshError = onAdvisoryRefreshError;
41
+ }
42
+ async getToken() {
43
+ // Stays set until a forced refresh succeeds.
44
+ const force = this.nextForce;
45
+ const cached = this.cached;
46
+ if (force || cached == null) {
47
+ return (await this.refresh(force)).token;
48
+ }
49
+ if (cached.expiresAt == null) {
50
+ return cached.token;
51
+ }
52
+ const remaining = cached.expiresAt - (0, types_1.nowAsSeconds)();
53
+ const advisory = Math.min(types_1.ADVISORY_REFRESH_THRESHOLD_IN_SECONDS, types_1.ADVISORY_REFRESH_LIFETIME_FRACTION * this.cachedLifetime);
54
+ const mandatory = Math.min(types_1.MANDATORY_REFRESH_THRESHOLD_IN_SECONDS, types_1.MANDATORY_REFRESH_LIFETIME_FRACTION * this.cachedLifetime);
55
+ if (remaining > advisory) {
56
+ return cached.token;
57
+ }
58
+ if (remaining > mandatory) {
59
+ this.backgroundRefresh();
60
+ return cached.token;
61
+ }
62
+ return (await this.refresh()).token;
63
+ }
64
+ /**
65
+ * Drops the cached token so the next {@link getToken} mints a new one.
66
+ * Call it after a 401: the server has rejected the token even though it
67
+ * has not expired.
68
+ *
69
+ * Pass the rejected token to leave the cache alone when it already holds a
70
+ * newer one, as happens when several requests fail with the same token.
71
+ */
72
+ invalidate(token) {
73
+ if (token !== undefined && this.cached?.token !== token)
74
+ return;
75
+ this.cached = null;
76
+ this.nextForce = true;
77
+ this.invalidations++;
78
+ }
79
+ /**
80
+ * Joins the refresh in flight, so the provider is never called twice at
81
+ * once. A forced refresh cannot join an unforced one, which may be served
82
+ * from the provider's own cache: it is queued to run when that call
83
+ * settles, and every caller that needs a token meanwhile waits for it.
84
+ */
85
+ refresh(force = false) {
86
+ if (this.queuedForcedRefresh) {
87
+ return this.queuedForcedRefresh;
88
+ }
89
+ const pending = this.pendingRefresh;
90
+ if (!pending) {
91
+ return this.doRefresh(force);
92
+ }
93
+ if (!force || this.pendingIsForced) {
94
+ return pending;
95
+ }
96
+ const run = () => {
97
+ this.queuedForcedRefresh = null;
98
+ return this.doRefresh(true);
99
+ };
100
+ this.queuedForcedRefresh = pending.then(run, run);
101
+ return this.queuedForcedRefresh;
102
+ }
103
+ /**
104
+ * Waits {@link ADVISORY_REFRESH_BACKOFF_IN_SECONDS} after a failure before
105
+ * trying again, so an outage during the advisory window does not send one
106
+ * token request per API request.
107
+ */
108
+ backgroundRefresh() {
109
+ if (this.pendingRefresh || this.queuedForcedRefresh) {
110
+ return;
111
+ }
112
+ if ((0, types_1.nowAsSeconds)() - this.lastAdvisoryError < types_1.ADVISORY_REFRESH_BACKOFF_IN_SECONDS) {
113
+ return;
114
+ }
115
+ this.doRefresh().catch((err) => {
116
+ this.lastAdvisoryError = (0, types_1.nowAsSeconds)();
117
+ this.onAdvisoryRefreshError?.(err);
118
+ });
119
+ }
120
+ /** Only called when no refresh is in flight. */
121
+ doRefresh(force = false) {
122
+ const invalidations = this.invalidations;
123
+ this.pendingIsForced = force;
124
+ this.pendingRefresh = (async () => this.provider(force ? { forceRefresh: true } : undefined))().then((token) => {
125
+ this.pendingRefresh = null;
126
+ // An unforced call that was already running when the cache was
127
+ // invalidated may return the rejected token from the provider's own
128
+ // cache. Its caller gets the result, but it is not cached, so the
129
+ // next caller forces a refresh.
130
+ if (force)
131
+ this.nextForce = false;
132
+ if (force || invalidations === this.invalidations) {
133
+ this.cached = token;
134
+ // Measured from now, not from the token's issue time, so a token
135
+ // that was slow to arrive is not already inside its refresh window.
136
+ this.cachedLifetime = token.expiresAt == null ? 0 : Math.max(0, token.expiresAt - (0, types_1.nowAsSeconds)());
137
+ }
138
+ return token;
139
+ }, (err) => {
140
+ this.pendingRefresh = null;
141
+ throw err;
142
+ });
143
+ return this.pendingRefresh;
144
+ }
145
+ }
146
+ exports.TokenCache = TokenCache;
147
+ //# sourceMappingURL=token-cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-cache.js","sourceRoot":"","sources":["../../src/lib/credentials/token-cache.ts"],"names":[],"mappings":";;;AACA,sCAOiB;AAEjB;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAa,UAAU;IAYrB,YAAY,QAA6B,EAAE,sBAA+C;QAVlF,WAAM,GAAuB,IAAI,CAAC;QAClC,mBAAc,GAAG,CAAC,CAAC;QACnB,mBAAc,GAAgC,IAAI,CAAC;QACnD,oBAAe,GAAG,KAAK,CAAC;QACxB,wBAAmB,GAAgC,IAAI,CAAC;QACxD,cAAS,GAAG,KAAK,CAAC;QAClB,kBAAa,GAAG,CAAC,CAAC;QAClB,sBAAiB,GAAG,CAAC,QAAQ,CAAC;QAIpC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,sBAAsB,GAAG,sBAAsB,CAAC;IACvD,CAAC;IAED,KAAK,CAAC,QAAQ;QACZ,6CAA6C;QAC7C,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC;QAC7B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAE3B,IAAI,KAAK,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;YAC5B,OAAO,C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C,SAAS,CAAC,CAAC,EAAE,CAAC,IAAI,CAClG,CAAC,KAAK,EAAE,EAAE;YACR,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;YAC3B,+DAA+D;YAC/D,oEAAoE;YACpE,kEAAkE;YAClE,gCAAgC;YAChC,IAAI,KAAK;gBAAE,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;YAClC,IAAI,KAAK,IAAI,aAAa,KAAK,IAAI,CAAC,aAAa,EAAE,CAAC;gBAClD,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;gBACpB,iEAAiE;gBACjE,oEAAoE;gBACpE,IAAI,CAAC,cAAc,GAAG,KAAK,CAAC,SAAS,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,SAAS,GAAG,IAAA,oBAAY,GAAE,CAAC,CAAC;YACpG,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC,EACD,CAAC,GAAG,EAAE,EAAE;YACN,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;YAC3B,MAAM,GAAG,CAAC;QACZ,CAAC,CACF,CAAC;QACF,OAAO,IAAI,CAAC,cAAc,CAAC;IAC7B,CAAC;CACF;AAzID,gCAyIC"}
@@ -0,0 +1,143 @@
1
+ import { ADVISORY_REFRESH_BACKOFF_IN_SECONDS, ADVISORY_REFRESH_LIFETIME_FRACTION, ADVISORY_REFRESH_THRESHOLD_IN_SECONDS, MANDATORY_REFRESH_LIFETIME_FRACTION, MANDATORY_REFRESH_THRESHOLD_IN_SECONDS, nowAsSeconds, } from "./types.mjs";
2
+ /**
3
+ * Wraps an {@link AccessTokenProvider} with two-tier proactive refresh and
4
+ * concurrent deduplication.
5
+ *
6
+ * The refresh thresholds scale with the token's lifetime (the time it had
7
+ * left when it was cached), so a token that lives for a few seconds is not
8
+ * refreshed on every call:
9
+ *
10
+ * - advisory: min(120s, 40% of the lifetime) remaining;
11
+ * - mandatory: min(30s, 10% of the lifetime) remaining.
12
+ *
13
+ * On each {@link getToken} call:
14
+ *
15
+ * - No cached token: call the provider, cache the result and return it.
16
+ * - Cached with `expiresAt == null`: return it forever.
17
+ * - More than the advisory threshold remaining: return the cached token.
18
+ * - Between the two thresholds: return the cached token and refresh in the
19
+ * background. A failure is reported to `onAdvisoryRefreshError` and the
20
+ * cached token keeps being served.
21
+ * - Less than the mandatory threshold remaining, or expired: wait for a
22
+ * refresh, and throw if it fails.
23
+ *
24
+ * Concurrent callers share one provider call.
25
+ */
26
+ export class TokenCache {
27
+ constructor(provider, onAdvisoryRefreshError) {
28
+ this.cached = null;
29
+ this.cachedLifetime = 0;
30
+ this.pendingRefresh = null;
31
+ this.pendingIsForced = false;
32
+ this.queuedForcedRefresh = null;
33
+ this.nextForce = false;
34
+ this.invalidations = 0;
35
+ this.lastAdvisoryError = -Infinity;
36
+ this.provider = provider;
37
+ this.onAdvisoryRefreshError = onAdvisoryRefreshError;
38
+ }
39
+ async getToken() {
40
+ // Stays set until a forced refresh succeeds.
41
+ const force = this.nextForce;
42
+ const cached = this.cached;
43
+ if (force || cached == null) {
44
+ return (await this.refresh(force)).token;
45
+ }
46
+ if (cached.expiresAt == null) {
47
+ return cached.token;
48
+ }
49
+ const remaining = cached.expiresAt - nowAsSeconds();
50
+ const advisory = Math.min(ADVISORY_REFRESH_THRESHOLD_IN_SECONDS, ADVISORY_REFRESH_LIFETIME_FRACTION * this.cachedLifetime);
51
+ const mandatory = Math.min(MANDATORY_REFRESH_THRESHOLD_IN_SECONDS, MANDATORY_REFRESH_LIFETIME_FRACTION * this.cachedLifetime);
52
+ if (remaining > advisory) {
53
+ return cached.token;
54
+ }
55
+ if (remaining > mandatory) {
56
+ this.backgroundRefresh();
57
+ return cached.token;
58
+ }
59
+ return (await this.refresh()).token;
60
+ }
61
+ /**
62
+ * Drops the cached token so the next {@link getToken} mints a new one.
63
+ * Call it after a 401: the server has rejected the token even though it
64
+ * has not expired.
65
+ *
66
+ * Pass the rejected token to leave the cache alone when it already holds a
67
+ * newer one, as happens when several requests fail with the same token.
68
+ */
69
+ invalidate(token) {
70
+ if (token !== undefined && this.cached?.token !== token)
71
+ return;
72
+ this.cached = null;
73
+ this.nextForce = true;
74
+ this.invalidations++;
75
+ }
76
+ /**
77
+ * Joins the refresh in flight, so the provider is never called twice at
78
+ * once. A forced refresh cannot join an unforced one, which may be served
79
+ * from the provider's own cache: it is queued to run when that call
80
+ * settles, and every caller that needs a token meanwhile waits for it.
81
+ */
82
+ refresh(force = false) {
83
+ if (this.queuedForcedRefresh) {
84
+ return this.queuedForcedRefresh;
85
+ }
86
+ const pending = this.pendingRefresh;
87
+ if (!pending) {
88
+ return this.doRefresh(force);
89
+ }
90
+ if (!force || this.pendingIsForced) {
91
+ return pending;
92
+ }
93
+ const run = () => {
94
+ this.queuedForcedRefresh = null;
95
+ return this.doRefresh(true);
96
+ };
97
+ this.queuedForcedRefresh = pending.then(run, run);
98
+ return this.queuedForcedRefresh;
99
+ }
100
+ /**
101
+ * Waits {@link ADVISORY_REFRESH_BACKOFF_IN_SECONDS} after a failure before
102
+ * trying again, so an outage during the advisory window does not send one
103
+ * token request per API request.
104
+ */
105
+ backgroundRefresh() {
106
+ if (this.pendingRefresh || this.queuedForcedRefresh) {
107
+ return;
108
+ }
109
+ if (nowAsSeconds() - this.lastAdvisoryError < ADVISORY_REFRESH_BACKOFF_IN_SECONDS) {
110
+ return;
111
+ }
112
+ this.doRefresh().catch((err) => {
113
+ this.lastAdvisoryError = nowAsSeconds();
114
+ this.onAdvisoryRefreshError?.(err);
115
+ });
116
+ }
117
+ /** Only called when no refresh is in flight. */
118
+ doRefresh(force = false) {
119
+ const invalidations = this.invalidations;
120
+ this.pendingIsForced = force;
121
+ this.pendingRefresh = (async () => this.provider(force ? { forceRefresh: true } : undefined))().then((token) => {
122
+ this.pendingRefresh = null;
123
+ // An unforced call that was already running when the cache was
124
+ // invalidated may return the rejected token from the provider's own
125
+ // cache. Its caller gets the result, but it is not cached, so the
126
+ // next caller forces a refresh.
127
+ if (force)
128
+ this.nextForce = false;
129
+ if (force || invalidations === this.invalidations) {
130
+ this.cached = token;
131
+ // Measured from now, not from the token's issue time, so a token
132
+ // that was slow to arrive is not already inside its refresh window.
133
+ this.cachedLifetime = token.expiresAt == null ? 0 : Math.max(0, token.expiresAt - nowAsSeconds());
134
+ }
135
+ return token;
136
+ }, (err) => {
137
+ this.pendingRefresh = null;
138
+ throw err;
139
+ });
140
+ return this.pendingRefresh;
141
+ }
142
+ }
143
+ //# sourceMappingURL=token-cache.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-cache.mjs","sourceRoot":"","sources":["../../src/lib/credentials/token-cache.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mCAAmC,EACnC,kCAAkC,EAClC,qCAAqC,EACrC,mCAAmC,EACnC,sCAAsC,EACtC,YAAY,GACb,oBAAgB;AAEjB;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,UAAU;IAYrB,YAAY,QAA6B,EAAE,sBAA+C;QAVlF,WAAM,GAAuB,IAAI,CAAC;QAClC,mBAAc,GAAG,CAAC,CAAC;QACnB,mBAAc,GAAgC,IAAI,CAAC;QACnD,oBAAe,GAAG,KAAK,CAAC;QACxB,wBAAmB,GAAgC,IAAI,CAAC;QACxD,cAAS,GAAG,KAAK,CAAC;QAClB,kBAAa,GAAG,CAAC,CAAC;QAClB,sBAAiB,GAAG,CAAC,QAAQ,CAAC;QAIpC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,sBAAsB,GAAG,sBAAsB,CAAC;IACvD,CAAC;IAED,KAAK,CAAC,QAAQ;QACZ,6CAA6C;QAC7C,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC;QAC7B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAE3B,IAAI,KAAK,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;YAC5B,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CAAC,EAAE,CAAC,IAAI,CAClG,CAAC,KAAK,EAAE,EAAE;YACR,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;YAC3B,+DAA+D;YAC/D,oEAAoE;YACpE,kEAAkE;YAClE,gCAAgC;YAChC,IAAI,KAAK;gBAAE,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;YAClC,IAAI,KAAK,IAAI,aAAa,KAAK,IAAI,CAAC,aAAa,EAAE,CAAC;gBAClD,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;gBACpB,iEAAiE;gBACjE,oEAAoE;gBACpE,IAAI,CAAC,cAAc,GAAG,KAAK,CAAC,SAAS,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,SAAS,GAAG,YAAY,EAAE,CAAC,CAAC;YACpG,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC,EACD,CAAC,GAAG,EAAE,EAAE;YACN,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;YAC3B,MAAM,GAAG,CAAC;QACZ,CAAC,CACF,CAAC;QACF,OAAO,IAAI,CAAC,cAAc,CAAC;IAC7B,CAAC;CACF"}
@@ -0,0 +1,126 @@
1
+ import { KernelError } from "../../core/error.mjs";
2
+ import type { Fetch } from "../../internal/builtin-types.mjs";
3
+ export type AccessToken = {
4
+ token: string;
5
+ /** Unix epoch seconds, fractions allowed. `null` means the token never expires. */
6
+ expiresAt: number | null;
7
+ };
8
+ /** What a client passes to its credentials on every call. */
9
+ export type CredentialsClientContext = {
10
+ /** The client's configured `fetch`, with the client's `fetchOptions` applied. */
11
+ fetch: Fetch;
12
+ /** The client's API base URL. */
13
+ baseURL: string;
14
+ };
15
+ export type AccessTokenProviderOptions = {
16
+ /**
17
+ * Set by {@link TokenCache.invalidate} after a 401. A provider that keeps
18
+ * its own cache must bypass it and mint a new token.
19
+ */
20
+ forceRefresh?: boolean;
21
+ /** Set when a Kernel client calls the provider. */
22
+ client?: CredentialsClientContext;
23
+ };
24
+ /** Mints an access token. Wrap it in a {@link TokenCache} to avoid minting one per request. */
25
+ export type AccessTokenProvider = (opts?: AccessTokenProviderOptions) => Promise<AccessToken>;
26
+ export type IdentityTokenRequest = {
27
+ /** The audience Kernel expects when the source has no explicit audience configured. */
28
+ audience?: string;
29
+ /** The `fetch` to use for sources that make an HTTP request. */
30
+ fetch?: Fetch;
31
+ };
32
+ /**
33
+ * Returns a token from the workload's own identity provider. It is called
34
+ * once per exchange and must return a fresh token every time: most providers'
35
+ * tokens can be exchanged only once.
36
+ */
37
+ export type IdentityTokenProvider = (request?: IdentityTokenRequest) => string | Promise<string>;
38
+ export type FederationPrincipal = {
39
+ type: string;
40
+ id: string;
41
+ };
42
+ export type FederationTokenDetails = {
43
+ /** Unix epoch seconds, fractions allowed. */
44
+ expiresAt: number;
45
+ /** The lifetime the token endpoint reported, in seconds. */
46
+ expiresIn: number;
47
+ scope: string | null;
48
+ orgId: string | null;
49
+ accessScope: string | null;
50
+ projectId: string | null;
51
+ principal: FederationPrincipal | null;
52
+ federationRuleId: string | null;
53
+ };
54
+ export type FederationAccessToken = {
55
+ token: string;
56
+ } & FederationTokenDetails;
57
+ export declare const GRANT_TYPE_JWT_BEARER = "urn:ietf:params:oauth:grant-type:jwt-bearer";
58
+ export declare const TOKEN_ENDPOINT = "/token";
59
+ export declare const DEFAULT_API_BASE_URL = "https://api.onkernel.com";
60
+ export declare const DEFAULT_AUTH_BASE_URL = "https://auth.onkernel.com";
61
+ export declare const ADVISORY_REFRESH_THRESHOLD_IN_SECONDS = 120;
62
+ export declare const ADVISORY_REFRESH_LIFETIME_FRACTION = 0.4;
63
+ export declare const MANDATORY_REFRESH_THRESHOLD_IN_SECONDS = 30;
64
+ export declare const MANDATORY_REFRESH_LIFETIME_FRACTION = 0.1;
65
+ export declare const ADVISORY_REFRESH_BACKOFF_IN_SECONDS = 5;
66
+ export declare const MAX_ASSERTION_BYTES: number;
67
+ export declare const MAX_TOKEN_RESPONSE_BYTES: number;
68
+ export declare const DEFAULT_CREDENTIALS_TIMEOUT_IN_MS = 30000;
69
+ export declare function nowAsSeconds(): number;
70
+ export declare class WorkloadIdentityError extends KernelError {
71
+ /** HTTP status of the token endpoint response, when there was one. */
72
+ readonly statusCode: number | null;
73
+ /** OAuth `error` code, such as `invalid_grant`. */
74
+ readonly errorCode: string | null;
75
+ /** OAuth `error_description`. */
76
+ readonly errorDescription: string | null;
77
+ readonly requestId: string | null;
78
+ /** The response body, reduced to its `error`, `error_description` and `error_uri` strings. */
79
+ readonly body: Record<string, string> | null;
80
+ constructor(message: string, { statusCode, errorCode, errorDescription, requestId, body, cause, }?: {
81
+ statusCode?: number | null;
82
+ errorCode?: string | null;
83
+ errorDescription?: string | null;
84
+ requestId?: string | null;
85
+ body?: Record<string, string> | null;
86
+ cause?: unknown;
87
+ });
88
+ }
89
+ /**
90
+ * Rejects token endpoints that would send an identity token over cleartext
91
+ * HTTP. Loopback hosts are allowed for local development.
92
+ */
93
+ export declare function requireSecureTokenEndpoint(baseURL: string): void;
94
+ /**
95
+ * The host that exchanges identity tokens: the explicit value, else
96
+ * `KERNEL_AUTH_BASE_URL`, else production when the API base URL is the
97
+ * production default. A custom API base URL never falls back to production.
98
+ */
99
+ export declare function resolveAuthBaseURL(explicit?: string | undefined, apiBaseURL?: string | undefined): string;
100
+ /** The `resource` an access token is requested for: the origin of the API base URL. */
101
+ export declare function resolveResource(explicit?: string | undefined, apiBaseURL?: string | undefined): string;
102
+ /**
103
+ * Reduces a token endpoint error body to what is safe to put in an error:
104
+ * the RFC 6749 error fields that are strings. Anything that is not a JSON
105
+ * object, such as a proxy's error page, is discarded. Pass every credential
106
+ * that was sent as `secrets`, so a server that echoes one back does not
107
+ * leak it.
108
+ */
109
+ export declare function redactSensitive(body: unknown, secrets?: string[]): Record<string, string> | null;
110
+ /**
111
+ * Describes an error thrown while a credential was in flight, with its
112
+ * causes and without the credential. Use the result in place of attaching
113
+ * the error as a `cause`, which loggers print in full.
114
+ */
115
+ export declare function describeFailure(err: unknown, secrets: string[]): string;
116
+ /** Replaces every occurrence of each secret, plain or URL-encoded, in text that came from outside the SDK. */
117
+ export declare function scrubSecrets(text: string, secrets: string[]): string;
118
+ /** Reads at most `maxBytes` of a response body as UTF-8 text. */
119
+ export declare function readLimitedText(response: Response, maxBytes?: number): Promise<string>;
120
+ /**
121
+ * Runs `fn` with an abort signal that fires after `timeoutMs`. The timer
122
+ * covers everything `fn` awaits, so it bounds reading the body as well as
123
+ * waiting for the headers.
124
+ */
125
+ export declare function withTimeout<T>(timeoutMs: number, fn: (signal: AbortSignal) => Promise<T>): Promise<T>;
126
+ //# sourceMappingURL=types.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../../src/lib/credentials/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,6BAAyB;AAC/C,OAAO,KAAK,EAAE,KAAK,EAAE,yCAAqC;AAG1D,MAAM,MAAM,WAAW,GAAG;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,mFAAmF;IACnF,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B,CAAC;AAEF,6DAA6D;AAC7D,MAAM,MAAM,wBAAwB,GAAG;IACrC,iFAAiF;IACjF,KAAK,EAAE,KAAK,CAAC;IACb,iCAAiC;IACjC,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,mDAAmD;IACnD,MAAM,CAAC,EAAE,wBAAwB,CAAC;CACnC,CAAC;AAEF,+FAA+F;AAC/F,MAAM,MAAM,mBAAmB,GAAG,CAAC,IAAI,CAAC,EAAE,0BAA0B,KAAK,OAAO,CAAC,WAAW,CAAC,CAAC;AAE9F,MAAM,MAAM,oBAAoB,GAAG;IACjC,uFAAuF;IACvF,QAAQ,C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,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAQtG;AAUD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,GAAE,MAAM,EAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,CAOpG;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAUvE;AAED,8GAA8G;AAC9G,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAQpE;AAED,iEAAiE;AACjE,wBAAsB,eAAe,CACnC,QAAQ,EAAE,QAAQ,EAClB,QAAQ,GAAE,MAAiC,GAC1C,OAAO,CAAC,MAAM,CAAC,CAwBjB;AAED;;;;GAIG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAQ3G"}
@@ -0,0 +1,126 @@
1
+ import { KernelError } from "../../core/error.js";
2
+ import type { Fetch } from "../../internal/builtin-types.js";
3
+ export type AccessToken = {
4
+ token: string;
5
+ /** Unix epoch seconds, fractions allowed. `null` means the token never expires. */
6
+ expiresAt: number | null;
7
+ };
8
+ /** What a client passes to its credentials on every call. */
9
+ export type CredentialsClientContext = {
10
+ /** The client's configured `fetch`, with the client's `fetchOptions` applied. */
11
+ fetch: Fetch;
12
+ /** The client's API base URL. */
13
+ baseURL: string;
14
+ };
15
+ export type AccessTokenProviderOptions = {
16
+ /**
17
+ * Set by {@link TokenCache.invalidate} after a 401. A provider that keeps
18
+ * its own cache must bypass it and mint a new token.
19
+ */
20
+ forceRefresh?: boolean;
21
+ /** Set when a Kernel client calls the provider. */
22
+ client?: CredentialsClientContext;
23
+ };
24
+ /** Mints an access token. Wrap it in a {@link TokenCache} to avoid minting one per request. */
25
+ export type AccessTokenProvider = (opts?: AccessTokenProviderOptions) => Promise<AccessToken>;
26
+ export type IdentityTokenRequest = {
27
+ /** The audience Kernel expects when the source has no explicit audience configured. */
28
+ audience?: string;
29
+ /** The `fetch` to use for sources that make an HTTP request. */
30
+ fetch?: Fetch;
31
+ };
32
+ /**
33
+ * Returns a token from the workload's own identity provider. It is called
34
+ * once per exchange and must return a fresh token every time: most providers'
35
+ * tokens can be exchanged only once.
36
+ */
37
+ export type IdentityTokenProvider = (request?: IdentityTokenRequest) => string | Promise<string>;
38
+ export type FederationPrincipal = {
39
+ type: string;
40
+ id: string;
41
+ };
42
+ export type FederationTokenDetails = {
43
+ /** Unix epoch seconds, fractions allowed. */
44
+ expiresAt: number;
45
+ /** The lifetime the token endpoint reported, in seconds. */
46
+ expiresIn: number;
47
+ scope: string | null;
48
+ orgId: string | null;
49
+ accessScope: string | null;
50
+ projectId: string | null;
51
+ principal: FederationPrincipal | null;
52
+ federationRuleId: string | null;
53
+ };
54
+ export type FederationAccessToken = {
55
+ token: string;
56
+ } & FederationTokenDetails;
57
+ export declare const GRANT_TYPE_JWT_BEARER = "urn:ietf:params:oauth:grant-type:jwt-bearer";
58
+ export declare const TOKEN_ENDPOINT = "/token";
59
+ export declare const DEFAULT_API_BASE_URL = "https://api.onkernel.com";
60
+ export declare const DEFAULT_AUTH_BASE_URL = "https://auth.onkernel.com";
61
+ export declare const ADVISORY_REFRESH_THRESHOLD_IN_SECONDS = 120;
62
+ export declare const ADVISORY_REFRESH_LIFETIME_FRACTION = 0.4;
63
+ export declare const MANDATORY_REFRESH_THRESHOLD_IN_SECONDS = 30;
64
+ export declare const MANDATORY_REFRESH_LIFETIME_FRACTION = 0.1;
65
+ export declare const ADVISORY_REFRESH_BACKOFF_IN_SECONDS = 5;
66
+ export declare const MAX_ASSERTION_BYTES: number;
67
+ export declare const MAX_TOKEN_RESPONSE_BYTES: number;
68
+ export declare const DEFAULT_CREDENTIALS_TIMEOUT_IN_MS = 30000;
69
+ export declare function nowAsSeconds(): number;
70
+ export declare class WorkloadIdentityError extends KernelError {
71
+ /** HTTP status of the token endpoint response, when there was one. */
72
+ readonly statusCode: number | null;
73
+ /** OAuth `error` code, such as `invalid_grant`. */
74
+ readonly errorCode: string | null;
75
+ /** OAuth `error_description`. */
76
+ readonly errorDescription: string | null;
77
+ readonly requestId: string | null;
78
+ /** The response body, reduced to its `error`, `error_description` and `error_uri` strings. */
79
+ readonly body: Record<string, string> | null;
80
+ constructor(message: string, { statusCode, errorCode, errorDescription, requestId, body, cause, }?: {
81
+ statusCode?: number | null;
82
+ errorCode?: string | null;
83
+ errorDescription?: string | null;
84
+ requestId?: string | null;
85
+ body?: Record<string, string> | null;
86
+ cause?: unknown;
87
+ });
88
+ }
89
+ /**
90
+ * Rejects token endpoints that would send an identity token over cleartext
91
+ * HTTP. Loopback hosts are allowed for local development.
92
+ */
93
+ export declare function requireSecureTokenEndpoint(baseURL: string): void;
94
+ /**
95
+ * The host that exchanges identity tokens: the explicit value, else
96
+ * `KERNEL_AUTH_BASE_URL`, else production when the API base URL is the
97
+ * production default. A custom API base URL never falls back to production.
98
+ */
99
+ export declare function resolveAuthBaseURL(explicit?: string | undefined, apiBaseURL?: string | undefined): string;
100
+ /** The `resource` an access token is requested for: the origin of the API base URL. */
101
+ export declare function resolveResource(explicit?: string | undefined, apiBaseURL?: string | undefined): string;
102
+ /**
103
+ * Reduces a token endpoint error body to what is safe to put in an error:
104
+ * the RFC 6749 error fields that are strings. Anything that is not a JSON
105
+ * object, such as a proxy's error page, is discarded. Pass every credential
106
+ * that was sent as `secrets`, so a server that echoes one back does not
107
+ * leak it.
108
+ */
109
+ export declare function redactSensitive(body: unknown, secrets?: string[]): Record<string, string> | null;
110
+ /**
111
+ * Describes an error thrown while a credential was in flight, with its
112
+ * causes and without the credential. Use the result in place of attaching
113
+ * the error as a `cause`, which loggers print in full.
114
+ */
115
+ export declare function describeFailure(err: unknown, secrets: string[]): string;
116
+ /** Replaces every occurrence of each secret, plain or URL-encoded, in text that came from outside the SDK. */
117
+ export declare function scrubSecrets(text: string, secrets: string[]): string;
118
+ /** Reads at most `maxBytes` of a response body as UTF-8 text. */
119
+ export declare function readLimitedText(response: Response, maxBytes?: number): Promise<string>;
120
+ /**
121
+ * Runs `fn` with an abort signal that fires after `timeoutMs`. The timer
122
+ * covers everything `fn` awaits, so it bounds reading the body as well as
123
+ * waiting for the headers.
124
+ */
125
+ export declare function withTimeout<T>(timeoutMs: number, fn: (signal: AbortSignal) => Promise<T>): Promise<T>;
126
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/credentials/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,4BAAyB;AAC/C,OAAO,KAAK,EAAE,KAAK,EAAE,wCAAqC;AAG1D,MAAM,MAAM,WAAW,GAAG;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,mFAAmF;IACnF,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B,CAAC;AAEF,6DAA6D;AAC7D,MAAM,MAAM,wBAAwB,GAAG;IACrC,iFAAiF;IACjF,KAAK,EAAE,KAAK,CAAC;IACb,iCAAiC;IACjC,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,mDAAmD;IACnD,MAAM,CAAC,EAAE,wBAAwB,CAAC;CACnC,CAAC;AAEF,+FAA+F;AAC/F,MAAM,MAAM,mBAAmB,GAAG,CAAC,IAAI,CAAC,EAAE,0BAA0B,KAAK,OAAO,CAAC,WAAW,CAAC,CAAC;AAE9F,MAAM,MAAM,oBAAoB,GAAG;IACjC,uFAAuF;IACvF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gEAAgE;IACh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