@vercel/flags-core 1.10.1 → 1.10.3

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.
@@ -576,7 +576,7 @@ function bulkEvaluate(flags, shared) {
576
576
  }
577
577
 
578
578
  // package.json
579
- var version = "1.10.1";
579
+ var version = "1.10.3";
580
580
 
581
581
  // src/lib/report-value.ts
582
582
  function internalReportValue(key, value, data) {
@@ -1104,19 +1104,28 @@ function unauthorizedMessage(sourceProjectId) {
1104
1104
  if (!sourceProjectId) {
1105
1105
  return "unauthorized (401)";
1106
1106
  }
1107
- return `unauthorized (401): this deployment is not allowed to read the flags of project "${sourceProjectId}". Check that the project is connected.`;
1107
+ return `unauthorized (401): this deployment is not allowed to read the flags of project "${sourceProjectId}". Check that the project is connected. See https://vercel.com/docs/flags/vercel-flags/project-connections`;
1108
1108
  }
1109
+ var oidcTokenMessage = [
1110
+ "@vercel/flags-core: Failed to get OIDC token.",
1111
+ "Are you running in a Vercel Environment where OIDC tokens are available?",
1112
+ "Did you mean to use an SDK Key instead? Use the environment variable FLAGS or pass it directly to the client."
1113
+ ].join(" ");
1114
+ var missingCredentials = /`vc link|vercel login|^The 'x-vercel-oidc-token' header is missing from the request\.$/;
1115
+ var OidcTokenError = class extends Error {
1116
+ constructor(cause) {
1117
+ super(oidcTokenMessage, { cause });
1118
+ this.name = "OidcTokenError";
1119
+ }
1120
+ };
1109
1121
  async function getOidcToken() {
1110
1122
  try {
1111
1123
  return await _oidc.getVercelOidcToken.call(void 0, );
1112
- } catch (e7) {
1113
- throw new Error(
1114
- [
1115
- "@vercel/flags-core: Failed to get OIDC token.",
1116
- "Are you running in a Vercel Environment where OIDC tokens are available?",
1117
- "Did you mean to use an SDK Key instead? Use the environment variable FLAGS or pass it directly to the client."
1118
- ].join(" ")
1119
- );
1124
+ } catch (error) {
1125
+ if (error instanceof Error && missingCredentials.test(error.message)) {
1126
+ throw new OidcTokenError(error);
1127
+ }
1128
+ throw new Error(oidcTokenMessage, { cause: error });
1120
1129
  }
1121
1130
  }
1122
1131
  function getProjectIdFromOidcToken(oidcToken) {
@@ -1159,13 +1168,24 @@ var Authentication = class {
1159
1168
  }
1160
1169
  return await getOidcToken();
1161
1170
  }
1162
- async resolveBundledDefinitionsLookup() {
1171
+ getStaticBundledDefinitionsLookup() {
1163
1172
  if (this.sdkKey) {
1164
1173
  return { type: "sdk-key", sdkKey: this.sdkKey };
1165
1174
  }
1166
1175
  if (this.sourceProjectId) {
1167
1176
  return { type: "project-id", projectId: this.sourceProjectId };
1168
1177
  }
1178
+ const projectId = process.env.VERCEL_PROJECT_ID;
1179
+ if (projectId) {
1180
+ return { type: "project-id", projectId };
1181
+ }
1182
+ return void 0;
1183
+ }
1184
+ async resolveBundledDefinitionsLookup() {
1185
+ const lookup = this.getStaticBundledDefinitionsLookup();
1186
+ if (lookup) {
1187
+ return lookup;
1188
+ }
1169
1189
  const oidcToken = await this.resolveToken();
1170
1190
  return {
1171
1191
  type: "project-id",
@@ -1235,7 +1255,7 @@ function getRuntimeIngest() {
1235
1255
  try {
1236
1256
  const context = globalThis[FLAGS_CONTEXT_SYMBOL];
1237
1257
  return typeof _optionalChain([context, 'optionalAccess', _34 => _34.ingest]) === "function" ? context.ingest : void 0;
1238
- } catch (e8) {
1258
+ } catch (e7) {
1239
1259
  return void 0;
1240
1260
  }
1241
1261
  }
@@ -1253,7 +1273,7 @@ async function getEvaluatingOidcToken(auth) {
1253
1273
  if (!auth.sdkKey) return void 0;
1254
1274
  try {
1255
1275
  return await _oidc.getVercelOidcToken.call(void 0, );
1256
- } catch (e9) {
1276
+ } catch (e8) {
1257
1277
  return void 0;
1258
1278
  }
1259
1279
  }
@@ -1365,7 +1385,7 @@ var Scheduler = (_class = class {
1365
1385
  })();
1366
1386
  try {
1367
1387
  this.scheduleTask(this.pending);
1368
- } catch (e10) {
1388
+ } catch (e9) {
1369
1389
  }
1370
1390
  this.maxTimeout = setTimeout(
1371
1391
  () => this.resolveScheduledFlush("max_timeout"),
@@ -1592,7 +1612,7 @@ var UsageTracker = (_class4 = class {
1592
1612
  void flush.finally(() => this.inflightFlushes.delete(flush));
1593
1613
  try {
1594
1614
  _optionalChain([this, 'access', _37 => _37.waitUntil, 'optionalCall', _38 => _38(flush)]);
1595
- } catch (e11) {
1615
+ } catch (e10) {
1596
1616
  }
1597
1617
  } else {
1598
1618
  this.scheduler.scheduleFlush();
@@ -1675,6 +1695,13 @@ var BundledSource = class {
1675
1695
  }
1676
1696
  return void 0;
1677
1697
  }
1698
+ /**
1699
+ * Starts reading bundled definitions without waiting for the result.
1700
+ */
1701
+ preload() {
1702
+ this.getResult().catch(() => {
1703
+ });
1704
+ }
1678
1705
  getResult() {
1679
1706
  if (!this.promise) {
1680
1707
  this.promise = this.options.readBundledDefinitions(this.options.auth);
@@ -1690,7 +1717,7 @@ function debug(clientName, event, details) {
1690
1717
  }
1691
1718
  try {
1692
1719
  console.debug("@vercel/flags-core", { event, clientName, ..._optionalChain([details, 'optionalCall', _40 => _40()]) });
1693
- } catch (e12) {
1720
+ } catch (e11) {
1694
1721
  }
1695
1722
  }
1696
1723
 
@@ -2029,7 +2056,7 @@ var DatafileCache = (_class5 = class {
2029
2056
  });
2030
2057
  try {
2031
2058
  this.waitUntil(background);
2032
- } catch (e13) {
2059
+ } catch (e12) {
2033
2060
  }
2034
2061
  }
2035
2062
  /** Resets storage, age, and the failure deadline so a restarted client begins clean. */
@@ -2142,7 +2169,8 @@ async function fetchDatafile(options) {
2142
2169
  return await Promise.race([fetchAttempt(attempt + 1), aborted]);
2143
2170
  } catch (error) {
2144
2171
  signal.throwIfAborted();
2145
- if (attempt === maxAttempts - 1 || error instanceof DatafileHttpError && !error.retryable) {
2172
+ if (attempt === maxAttempts - 1 || error instanceof DatafileHttpError && !error.retryable || // Retrying will not produce a token that is not there.
2173
+ error instanceof OidcTokenError) {
2146
2174
  throw error instanceof Error ? error : new Error("Unknown fetch error");
2147
2175
  }
2148
2176
  debug(options.clientName, "datafile.fetch.retry", () => ({
@@ -2560,7 +2588,7 @@ async function connectStream(config, callbacks) {
2560
2588
  rejectInit(error);
2561
2589
  }
2562
2590
  _optionalChain([onDisconnect, 'optionalCall', _97 => _97()]);
2563
- abortController.abort();
2591
+ abortController.abort(error);
2564
2592
  break;
2565
2593
  }
2566
2594
  if (!response.ok) {
@@ -2596,7 +2624,7 @@ async function connectStream(config, callbacks) {
2596
2624
  let message;
2597
2625
  try {
2598
2626
  message = JSON.parse(line);
2599
- } catch (e14) {
2627
+ } catch (e13) {
2600
2628
  console.warn(
2601
2629
  "@vercel/flags-core: Failed to parse stream message, skipping"
2602
2630
  );
@@ -2662,7 +2690,7 @@ async function connectStream(config, callbacks) {
2662
2690
  }
2663
2691
  if (error instanceof TokenResolutionError && !initialDataReceived) {
2664
2692
  rejectInit(error.cause);
2665
- abortController.abort();
2693
+ abortController.abort(error.cause);
2666
2694
  break;
2667
2695
  }
2668
2696
  if (connectionAbort.signal.reason !== PING_TIMEOUT) {
@@ -2736,7 +2764,7 @@ var StreamSource = (_class9 = class extends TypedEmitter {
2736
2764
  if (this.abortController === abortController) {
2737
2765
  this.promise = void 0;
2738
2766
  this.abortController = void 0;
2739
- this.emit("exhausted");
2767
+ this.emit("exhausted", abortController.signal.reason);
2740
2768
  }
2741
2769
  },
2742
2770
  { once: true }
@@ -2797,7 +2825,7 @@ var UNKNOWN_FRESHNESS = {
2797
2825
  assess: () => ({ status: "unknown" })
2798
2826
  };
2799
2827
  function isUnauthorizedError(error) {
2800
- if (error instanceof UnauthorizedError) {
2828
+ if (error instanceof UnauthorizedError || error instanceof OidcTokenError) {
2801
2829
  return true;
2802
2830
  }
2803
2831
  if (!(error instanceof Error)) {
@@ -2832,8 +2860,9 @@ var Controller = (_class10 = class {
2832
2860
  // Build-step deduplication
2833
2861
  __init23() {this.buildDataPromise = null}
2834
2862
  __init24() {this.buildReadTracked = false}
2835
- // Suppresses usage tracking while the credential is rejected. Set on a 401
2836
- // from any source, cleared as soon as a source delivers data again.
2863
+ // Suppresses usage tracking while the credential is rejected or no OIDC
2864
+ // token is available. Set on an auth failure from any source, cleared as
2865
+ // soon as a source delivers data again.
2837
2866
  __init25() {this.unauthorized = false}
2838
2867
  constructor(options) {;_class10.prototype.__init18.call(this);_class10.prototype.__init19.call(this);_class10.prototype.__init20.call(this);_class10.prototype.__init21.call(this);_class10.prototype.__init22.call(this);_class10.prototype.__init23.call(this);_class10.prototype.__init24.call(this);_class10.prototype.__init25.call(this);_class10.prototype.__init26.call(this);_class10.prototype.__init27.call(this);_class10.prototype.__init28.call(this);_class10.prototype.__init29.call(this);_class10.prototype.__init30.call(this);_class10.prototype.__init31.call(this);_class10.prototype.__init32.call(this);_class10.prototype.__init33.call(this);_class10.prototype.__init34.call(this);
2839
2868
  this.options = normalizeOptions(options);
@@ -2870,6 +2899,9 @@ var Controller = (_class10 = class {
2870
2899
  auth: this.options.auth,
2871
2900
  readBundledDefinitions
2872
2901
  });
2902
+ if (!this.options.datafile && this.options.auth.getStaticBundledDefinitionsLookup()) {
2903
+ this.bundledSource.preload();
2904
+ }
2873
2905
  this.wireSourceEvents();
2874
2906
  if (this.options.datafile) {
2875
2907
  this.cache.seed(tagData({ ...this.options.datafile }, "provided"));
@@ -2926,10 +2958,16 @@ var Controller = (_class10 = class {
2926
2958
  this.transition("degraded", "stream-disconnected");
2927
2959
  }
2928
2960
  }}
2929
- __init31() {this.onStreamExhausted = () => {
2961
+ __init31() {this.onStreamExhausted = (reason) => {
2930
2962
  if (this.state !== "streaming" && this.state !== "degraded") {
2931
2963
  return;
2932
2964
  }
2965
+ if (isUnauthorizedError(reason)) {
2966
+ if (this.state === "streaming") {
2967
+ this.transition("degraded", "stream-auth-failed");
2968
+ }
2969
+ return;
2970
+ }
2933
2971
  void this.activateFallbackSource().catch(() => {
2934
2972
  });
2935
2973
  }}
@@ -2941,7 +2979,7 @@ var Controller = (_class10 = class {
2941
2979
  this.onSourceError(error);
2942
2980
  if (this.state === "polling" && isUnauthorizedError(error)) {
2943
2981
  this.pollingSource.stop();
2944
- this.transition("degraded", "polling-unauthorized");
2982
+ this.transition("degraded", "polling-auth-failed");
2945
2983
  }
2946
2984
  }}
2947
2985
  // ---------------------------------------------------------------------------
@@ -3070,7 +3108,7 @@ var Controller = (_class10 = class {
3070
3108
  try {
3071
3109
  const bundled = await this.bundledSource.tryLoad();
3072
3110
  if (bundled) this.cache.seed(tagData({ ...bundled }, "bundled"));
3073
- } catch (e15) {
3111
+ } catch (e14) {
3074
3112
  }
3075
3113
  }
3076
3114
  if (this.headerSource.isEnabled()) {
@@ -3255,7 +3293,7 @@ var Controller = (_class10 = class {
3255
3293
  }
3256
3294
  try {
3257
3295
  await this.cache.refresh();
3258
- } catch (e16) {
3296
+ } catch (e15) {
3259
3297
  throw this.noDefinitionsError(
3260
3298
  ". Initialize the client or provide a datafile."
3261
3299
  );
@@ -3351,6 +3389,10 @@ var Controller = (_class10 = class {
3351
3389
  this.transition("degraded", "stream-still-connecting");
3352
3390
  return;
3353
3391
  }
3392
+ if (outcome === "auth-failed") {
3393
+ this.transition("degraded", "stream-auth-failed");
3394
+ return;
3395
+ }
3354
3396
  }
3355
3397
  if (this.options.polling.enabled) {
3356
3398
  if (this.state !== "polling") {
@@ -3383,7 +3425,7 @@ var Controller = (_class10 = class {
3383
3425
  return "connected";
3384
3426
  } catch (error) {
3385
3427
  this.noteUnauthorized(error);
3386
- return "failed";
3428
+ return isUnauthorizedError(error) ? "auth-failed" : "failed";
3387
3429
  }
3388
3430
  }
3389
3431
  let timeoutId;
@@ -3417,7 +3459,7 @@ var Controller = (_class10 = class {
3417
3459
  } catch (error) {
3418
3460
  clearTimeout(timeoutId);
3419
3461
  this.noteUnauthorized(error);
3420
- return "failed";
3462
+ return isUnauthorizedError(error) ? "auth-failed" : "failed";
3421
3463
  }
3422
3464
  }
3423
3465
  // ---------------------------------------------------------------------------
@@ -3568,7 +3610,7 @@ var Controller = (_class10 = class {
3568
3610
  this.acceptLastResortFetch(await this.fetchOnce());
3569
3611
  this.transition("degraded", "fetched-fallback");
3570
3612
  return;
3571
- } catch (e17) {
3613
+ } catch (e16) {
3572
3614
  }
3573
3615
  }
3574
3616
  throw this.noDefinitionsError(". Bundled definitions not found.");
@@ -3613,7 +3655,7 @@ var Controller = (_class10 = class {
3613
3655
  let fetched;
3614
3656
  try {
3615
3657
  fetched = await this.fetchOnce();
3616
- } catch (e18) {
3658
+ } catch (e17) {
3617
3659
  }
3618
3660
  if (fetched) {
3619
3661
  const remote = this.acceptLastResortFetch(fetched);
@@ -3745,4 +3787,4 @@ var {
3745
3787
 
3746
3788
 
3747
3789
  exports.ResolutionReason = ResolutionReason; exports.evaluate = evaluate; exports.FallbackNotFoundError = FallbackNotFoundError; exports.FallbackEntryNotFoundError = FallbackEntryNotFoundError; exports.Controller = Controller; exports.flagsClient = flagsClient; exports.resetDefaultFlagsClient = resetDefaultFlagsClient; exports.createClient = createClient;
3748
- //# sourceMappingURL=chunk-EW4YJU7V.cjs.map
3790
+ //# sourceMappingURL=chunk-BEQ3MX7Z.cjs.map