@friggframework/core 2.0.0--canary.744.9a69fb7.0 → 2.0.0--canary.744.e18e27f.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.
@@ -66,11 +66,12 @@ class IntegrationRepositoryMongo extends IntegrationRepositoryInterface {
66
66
  * @returns {Promise<Array>} Array of integration objects (possibly empty)
67
67
  */
68
68
  async findIntegrations({ type, status } = {}) {
69
- const where = {};
69
+ let where = {};
70
70
  if (type) {
71
- where.config = { path: ['type'], equals: type };
72
- }
73
- if (status) {
71
+ const ids = await this._findIdsByType(type, { status });
72
+ if (ids.length === 0) return [];
73
+ where = { id: { in: ids } };
74
+ } else if (status) {
74
75
  where.status = status;
75
76
  }
76
77
 
@@ -93,6 +94,28 @@ class IntegrationRepositoryMongo extends IntegrationRepositoryInterface {
93
94
  }));
94
95
  }
95
96
 
97
+ /**
98
+ * Ids of the integrations whose config.type is `type`. Prisma's MongoDB
99
+ * connector has no JSON path filter (a where on config.type throws), so
100
+ * the filter runs as a raw query and returns the ids only.
101
+ *
102
+ * @param {string} type - Integration type (config.type)
103
+ * @param {Object} [options]
104
+ * @param {string} [options.status] - Integration status
105
+ * @param {number} [options.limit] - Most ids to return
106
+ * @returns {Promise<string[]>}
107
+ * @private
108
+ */
109
+ async _findIdsByType(type, { status, limit } = {}) {
110
+ const filter = { 'config.type': type };
111
+ if (status) filter.status = status;
112
+ const options = { projection: { _id: 1 } };
113
+ if (limit) options.limit = limit;
114
+
115
+ const docs = await this.prisma.integration.findRaw({ filter, options });
116
+ return (docs ?? []).map(({ _id }) => String(_id?.$oid ?? _id));
117
+ }
118
+
96
119
  /**
97
120
  * Find every integration in a report-shaped projection.
98
121
  *
@@ -153,17 +176,13 @@ class IntegrationRepositoryMongo extends IntegrationRepositoryInterface {
153
176
  * @returns {Promise<Object>} Integration object
154
177
  */
155
178
  async findIntegrationByName(name) {
156
- const integration = await this.prisma.integration.findFirst({
157
- where: {
158
- config: {
159
- path: ['type'],
160
- equals: name,
161
- },
162
- },
163
- include: {
164
- entities: true,
165
- },
166
- });
179
+ const [id] = await this._findIdsByType(name, { limit: 1 });
180
+ const integration = id
181
+ ? await this.prisma.integration.findUnique({
182
+ where: { id },
183
+ include: { entities: true },
184
+ })
185
+ : null;
167
186
 
168
187
  if (!integration) {
169
188
  throw new Error(`Integration with name ${name} not found`);
@@ -136,23 +136,33 @@ class OAuth2Requester extends Requester {
136
136
  : 0;
137
137
  }
138
138
 
139
+ /**
140
+ * True when the module opted in (`refreshBeforeExpiryMs` > 0) and a
141
+ * refresh token is held. Off for the `client_credentials` grant:
142
+ * getTokenFromClientCredentials() reports any failure, a 503 too, as
143
+ * invalid auth, which would disable a working credential. Without a
144
+ * refresh token, the provider rejects the refresh, which would also mark
145
+ * the credential invalid.
146
+ *
147
+ * @returns {boolean}
148
+ */
149
+ isProactiveRefreshEnabled() {
150
+ if (!this.refreshBeforeExpiryMs) return false;
151
+ if (this.grant_type === 'client_credentials') return false;
152
+ return Boolean(this.refresh_token);
153
+ }
154
+
139
155
  /**
140
156
  * True when proactive refresh is on and the access token expires within
141
157
  * `refreshBeforeExpiryMs`. `accessTokenExpire` can be a Date, or an ISO
142
158
  * string from the credential store. An unknown or unreadable expiry
143
159
  * returns false: the 401 path handles that token.
144
160
  *
145
- * Only for a grant with a refresh token. The client_credentials grant is
146
- * left out: getTokenFromClientCredentials() reports any failure,
147
- * a 503 too, as invalid auth, which would disable a working credential.
148
- *
149
161
  * @returns {boolean}
150
162
  */
151
163
  _isTokenExpiringSoon() {
152
- if (!this.refreshBeforeExpiryMs) return false;
164
+ if (!this.isProactiveRefreshEnabled()) return false;
153
165
  if (!this.accessTokenExpire) return false;
154
- if (this.grant_type === 'client_credentials') return false;
155
- if (!this.refresh_token) return false;
156
166
  const expiresAt = new Date(this.accessTokenExpire).getTime();
157
167
  if (Number.isNaN(expiresAt)) return false;
158
168
  return expiresAt - Date.now() <= this.refreshBeforeExpiryMs;
@@ -789,9 +789,10 @@ class Requester extends Delegate {
789
789
  * A failure here does not fail the request. The request goes out with
790
790
  * the current token, and the 401 path stays the fallback. After a
791
791
  * failure, no proactive refresh runs for PROACTIVE_REFRESH_COOLDOWN_MS.
792
- * The same cooldown applies after a refresh whose new token still
793
- * expires inside the window (no `expires_in`, or a lifetime shorter
794
- * than the window). Without it, every request would refresh.
792
+ * After a refresh whose new token still expires inside the window (no
793
+ * `expires_in`, or a lifetime shorter than the window), the proactive
794
+ * refresh is off for this instance. Without that, every request would
795
+ * refresh.
795
796
  * Concurrent callers share one refresh through _refreshAuthOnce().
796
797
  *
797
798
  * @param {string} url - The request URL. A call to the token endpoint
@@ -819,14 +820,17 @@ class Requester extends Delegate {
819
820
  }
820
821
  if (refreshed) {
821
822
  if (this._isTokenExpiringSoon()) {
823
+ // No expires_in, or a lifetime shorter than the window: the
824
+ // new token always looks expiring, so a cooldown would only
825
+ // space out a refresh on every request. Turn the proactive
826
+ // refresh off for this instance. The 401 path stays.
822
827
  this.logger.warn(
823
- 'The refreshed token expires inside the proactive refresh window',
828
+ 'The refreshed token expires inside the proactive refresh window, proactive refresh is off for this instance',
824
829
  {
825
830
  eventName: `${this.logger.name}.proactive_refresh_window_too_long`,
826
831
  }
827
832
  );
828
- this._proactiveRefreshNotBefore =
829
- Date.now() + PROACTIVE_REFRESH_COOLDOWN_MS;
833
+ this._proactiveRefreshNotBefore = Infinity;
830
834
  }
831
835
  return;
832
836
  }
@@ -840,6 +844,32 @@ class Requester extends Delegate {
840
844
  }
841
845
  }
842
846
 
847
+ /**
848
+ * Refreshes the token now, through the same path as the refresh after a
849
+ * 401: one refresh at a time per instance (concurrent requests share
850
+ * it), inside the refresh marker (so the refresh's own requests never
851
+ * start a proactive refresh), and the auth generation advances. For
852
+ * scheduled refreshers, such as the built-in `refresh-oauth-tokens`
853
+ * admin script. Call refreshAuth() directly only from inside a refresh.
854
+ *
855
+ * @returns {Promise<boolean>} True if the instance holds a usable token.
856
+ */
857
+ refreshAuthNow() {
858
+ return this._refreshAuthOnce();
859
+ }
860
+
861
+ /**
862
+ * True when a scheduled refresher may refresh this instance's token
863
+ * ahead of expiry. Off by default, for the same reason as
864
+ * _isTokenExpiringSoon(). OAuth2Requester turns it on with
865
+ * `refreshBeforeExpiryMs`.
866
+ *
867
+ * @returns {boolean}
868
+ */
869
+ isProactiveRefreshEnabled() {
870
+ return false;
871
+ }
872
+
843
873
  /**
844
874
  * Hook for requesters that know when their token expires. Return true
845
875
  * to refresh before the next request. Off by default, because a
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@friggframework/core",
3
3
  "prettier": "@friggframework/prettier-config",
4
- "version": "2.0.0--canary.744.9a69fb7.0",
4
+ "version": "2.0.0--canary.744.e18e27f.0",
5
5
  "dependencies": {
6
6
  "@aws-sdk/client-apigatewaymanagementapi": "^3.588.0",
7
7
  "@aws-sdk/client-kms": "^3.588.0",
@@ -11,7 +11,7 @@
11
11
  "@aws-sdk/client-sqs": "^3.588.0",
12
12
  "@aws-sdk/client-ssm": "^3.588.0",
13
13
  "@aws-sdk/s3-request-presigner": "^3.588.0",
14
- "@friggframework/schemas": "2.0.0--canary.744.9a69fb7.0",
14
+ "@friggframework/schemas": "2.0.0--canary.744.e18e27f.0",
15
15
  "@hapi/boom": "^10.0.1",
16
16
  "@opentelemetry/api": "^1.9.1",
17
17
  "@opentelemetry/context-async-hooks": "^2.9.0",
@@ -49,9 +49,9 @@
49
49
  }
50
50
  },
51
51
  "devDependencies": {
52
- "@friggframework/eslint-config": "2.0.0--canary.744.9a69fb7.0",
53
- "@friggframework/prettier-config": "2.0.0--canary.744.9a69fb7.0",
54
- "@friggframework/test": "2.0.0--canary.744.9a69fb7.0",
52
+ "@friggframework/eslint-config": "2.0.0--canary.744.e18e27f.0",
53
+ "@friggframework/prettier-config": "2.0.0--canary.744.e18e27f.0",
54
+ "@friggframework/test": "2.0.0--canary.744.e18e27f.0",
55
55
  "@prisma/client": "^6.19.3",
56
56
  "@types/lodash": "4.17.15",
57
57
  "@typescript-eslint/eslint-plugin": "^8.0.0",
@@ -93,5 +93,5 @@
93
93
  "publishConfig": {
94
94
  "access": "public"
95
95
  },
96
- "gitHead": "9a69fb7c1880d0a1d7575e2e18a9fcf0fc76f37c"
96
+ "gitHead": "e18e27f133f0afa7723d361b61ef8e7e80a830ef"
97
97
  }
@@ -117,6 +117,10 @@ declare module "@friggframework/module-plugin" {
117
117
  _put(options: RequestOptions): Promise<any>;
118
118
  _delete(options: RequestOptions): Promise<any>;
119
119
  refreshAuth(): Promise<any>;
120
+ /** Refreshes now through the shared refresh that the 401 retry uses. */
121
+ refreshAuthNow(): Promise<boolean>;
122
+ /** True when a scheduled refresher may refresh ahead of expiry. False by default. */
123
+ isProactiveRefreshEnabled(): boolean;
120
124
  _adoptNewerCredential(): Promise<boolean>;
121
125
  }
122
126
 
@@ -209,6 +213,11 @@ declare module "@friggframework/module-plugin" {
209
213
 
210
214
  constructor(params: OAuth2RequesterConstructor);
211
215
 
216
+ /** True when opted in, a refresh token is held, and the grant is not client_credentials. */
217
+ isProactiveRefreshEnabled(): boolean;
218
+ /** Refreshes now through the shared refresh that the 401 retry uses. */
219
+ refreshAuthNow(): Promise<boolean>;
220
+
212
221
  addAuthHeaders(headers: object): Promise<object>;
213
222
  getAuthorizationUri(): string;
214
223
  getTokenFromClientCredentials(): Promise<Token>;