@vercel/flags-core 1.5.0-dde2adb-20260526050633 → 1.5.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.
package/CHANGELOG.md CHANGED
@@ -1,10 +1,22 @@
1
1
  # @vercel/flags-core
2
2
 
3
- ## 1.5.0-dde2adb-20260526050633
3
+ ## 1.5.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#395](https://github.com/vercel/flags/pull/395) [`b0150af`](https://github.com/vercel/flags/commit/b0150af9c8190f0db0efc25409fab89769cab6a7) Thanks [@lucleray](https://github.com/lucleray)! - Reduce log noise from stream reconnects.
8
+
9
+ Retryable stream errors are no longer logged on every failed attempt; the
10
+ underlying error is now surfaced only once retries are exhausted (via the
11
+ existing "Max retry count exceeded" log). The stream/polling initialization
12
+ timeout warnings were also reworded to make clear the client keeps connecting
13
+ in the background while serving fallback values.
14
+
15
+ ## 1.5.0
4
16
 
5
17
  ### Minor Changes
6
18
 
7
- - [#385](https://github.com/vercel/flags/pull/385) [`5b2dc37`](https://github.com/vercel/flags/commit/5b2dc37041e24811f735beede41fc8c29f508cb7) Thanks [@dferber90](https://github.com/dferber90)! - Add `bulkEvaluate` method to `FlagsClient` for resolving multiple flags against shared entities in a single call.
19
+ - [#385](https://github.com/vercel/flags/pull/385) [`201f9d5`](https://github.com/vercel/flags/commit/201f9d5988d7fc307511e35638e66769d38cedb3) Thanks [@dferber90](https://github.com/dferber90)! - Add `bulkEvaluate` method to `FlagsClient` for resolving multiple flags against shared entities in a single call.
8
20
 
9
21
  ```ts
10
22
  const results = await client.bulkEvaluate(
@@ -29,6 +41,14 @@
29
41
  processes. When all retry attempts are exhausted the SDK now logs a structured
30
42
  warning so consumers can alert on dropped batches.
31
43
 
44
+ - [#390](https://github.com/vercel/flags/pull/390) [`7b5ea9a`](https://github.com/vercel/flags/commit/7b5ea9a808dfd4155bd2bbf321c3b44ec730cda6) Thanks [@luismeyer](https://github.com/luismeyer)! - Add OIDC authentication support for Vercel Flags clients and generated flag definitions.
45
+
46
+ `@vercel/flags-core` can now create clients without an SDK key and authenticate with a Vercel OIDC token, while still supporting SDK keys and connection strings. Bundled definitions can be looked up by SDK key hash or OIDC project ID.
47
+
48
+ `@vercel/prepare-flags-definitions` now collects both SDK keys and `VERCEL_OIDC_TOKEN`, fetches definitions for each auth entry, deduplicates identical definitions across SDK keys and OIDC project IDs, and writes generated maps keyed by SDK key hash or project ID.
49
+
50
+ `@flags-sdk/vercel` now supports provider data lookup for Vercel flag origins that do not include an SDK key, allowing OIDC-backed clients to resolve project metadata.
51
+
32
52
  ### Patch Changes
33
53
 
34
54
  - [#382](https://github.com/vercel/flags/pull/382) [`4d90e91`](https://github.com/vercel/flags/commit/4d90e912a4d7c9d4ef986d5e8dc609c30b203242) Thanks [@dferber90](https://github.com/dferber90)! - Speed up flag evaluation on the hot path.
@@ -467,7 +467,7 @@ function findWeightedIndex(weights, value, maxValue) {
467
467
  }
468
468
 
469
469
  // package.json
470
- var version = "1.5.0-dde2adb-20260526050633";
470
+ var version = "1.5.1";
471
471
 
472
472
  // src/lib/report-value.ts
473
473
  function internalReportValue(key, value, data) {
@@ -744,7 +744,7 @@ function hashSdkKey(sdkKey) {
744
744
  sdkKeyHashCache.set(sdkKey, promise);
745
745
  return promise;
746
746
  }
747
- async function readBundledDefinitions(sdkKey) {
747
+ async function readBundledDefinitions(auth) {
748
748
  let get;
749
749
  try {
750
750
  const module = await import(
@@ -761,10 +761,20 @@ async function readBundledDefinitions(sdkKey) {
761
761
  if (typeof get !== "function") {
762
762
  return { definitions: null, state: "missing-file" };
763
763
  }
764
- const entry = get(sdkKey);
764
+ let lookup;
765
+ try {
766
+ lookup = await auth.resolveBundledDefinitionsLookup();
767
+ } catch (error) {
768
+ return { definitions: null, state: "unexpected-error", error };
769
+ }
770
+ if (lookup.type === "project-id") {
771
+ const entry2 = get(lookup.projectId);
772
+ return entry2 ? { definitions: entry2, state: "ok" } : { definitions: null, state: "missing-entry" };
773
+ }
774
+ const entry = get(lookup.sdkKey);
765
775
  if (entry) return { definitions: entry, state: "ok" };
766
776
  try {
767
- const hashedKey = await hashSdkKey(sdkKey);
777
+ const hashedKey = await hashSdkKey(lookup.sdkKey);
768
778
  const hashedEntry = get(hashedKey);
769
779
  if (hashedEntry) return { definitions: hashedEntry, state: "ok" };
770
780
  } catch (error) {
@@ -935,13 +945,14 @@ var UsageTracker = class {
935
945
  const flushId = ++this.flushCounter;
936
946
  for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
937
947
  try {
948
+ const token = await this.options.auth.resolveToken();
938
949
  const response = await this.options.fetch(
939
950
  `${this.options.host}/v1/ingest`,
940
951
  {
941
952
  method: "POST",
942
953
  headers: {
943
954
  "Content-Type": "application/json",
944
- Authorization: `Bearer ${this.options.sdkKey}`,
955
+ Authorization: `Bearer ${token}`,
945
956
  "User-Agent": `VercelFlagsCore/${version}`,
946
957
  ...process.env.VERCEL_ENV ? { "X-Vercel-Env": process.env.VERCEL_ENV } : null,
947
958
  ...isDebugMode ? { "x-vercel-debug-ingest": "1" } : null
@@ -993,11 +1004,10 @@ var FallbackEntryNotFoundError = class extends Error {
993
1004
 
994
1005
  // src/controller/bundled-source.ts
995
1006
  var BundledSource = class {
996
- promise;
997
- options;
998
1007
  constructor(options) {
999
1008
  this.options = options;
1000
1009
  }
1010
+ promise;
1001
1011
  /**
1002
1012
  * Load bundled definitions.
1003
1013
  * Throws if bundled definitions are not available.
@@ -1042,7 +1052,7 @@ var BundledSource = class {
1042
1052
  }
1043
1053
  getResult() {
1044
1054
  if (!this.promise) {
1045
- this.promise = this.options.readBundledDefinitions(this.options.sdkKey);
1055
+ this.promise = this.options.readBundledDefinitions(this.options.auth);
1046
1056
  }
1047
1057
  return this.promise;
1048
1058
  }
@@ -1051,6 +1061,7 @@ var BundledSource = class {
1051
1061
  // src/controller/fetch-datafile.ts
1052
1062
  var DEFAULT_FETCH_TIMEOUT_MS = 1e4;
1053
1063
  async function fetchDatafile(options) {
1064
+ const token = await options.auth.resolveToken();
1054
1065
  const controller = new AbortController();
1055
1066
  const timeoutId = setTimeout(
1056
1067
  () => controller.abort(),
@@ -1067,7 +1078,7 @@ async function fetchDatafile(options) {
1067
1078
  try {
1068
1079
  const res = await options.fetch(`${options.host}/v1/datafile`, {
1069
1080
  headers: {
1070
- Authorization: `Bearer ${options.sdkKey}`,
1081
+ Authorization: `Bearer ${token}`,
1071
1082
  "User-Agent": `VercelFlagsCore/${version}`,
1072
1083
  ...process.env.VERCEL_ENV ? { "X-Vercel-Env": process.env.VERCEL_ENV } : null
1073
1084
  },
@@ -1124,7 +1135,7 @@ function normalizeOptions(options) {
1124
1135
  };
1125
1136
  }
1126
1137
  return {
1127
- sdkKey: options.sdkKey,
1138
+ auth: options.auth,
1128
1139
  datafile: options.datafile,
1129
1140
  stream,
1130
1141
  polling,
@@ -1234,13 +1245,14 @@ var UnauthorizedError = class extends Error {
1234
1245
  this.name = "UnauthorizedError";
1235
1246
  }
1236
1247
  };
1248
+ var TokenResolutionError = class extends Error {
1249
+ constructor(error) {
1250
+ super("stream: token resolution failed", { cause: error });
1251
+ this.name = "TokenResolutionError";
1252
+ }
1253
+ };
1237
1254
  async function connectStream(config, callbacks) {
1238
- const {
1239
- host,
1240
- sdkKey,
1241
- abortController,
1242
- fetch: fetchFn = globalThis.fetch
1243
- } = config;
1255
+ const { host, abortController, fetch: fetchFn = globalThis.fetch } = config;
1244
1256
  const { onDatafile, onPrimed, onDisconnect } = callbacks;
1245
1257
  let retryCount = 0;
1246
1258
  let lastAttemptTime = 0;
@@ -1252,9 +1264,13 @@ async function connectStream(config, callbacks) {
1252
1264
  });
1253
1265
  void (async () => {
1254
1266
  let initialDataReceived = false;
1267
+ let lastError;
1255
1268
  while (!abortController.signal.aborted) {
1256
1269
  if (retryCount > MAX_RETRY_COUNT) {
1257
- console.error("@vercel/flags-core: Max retry count exceeded");
1270
+ console.error(
1271
+ "@vercel/flags-core: Max retry count exceeded",
1272
+ lastError ?? "stream closed repeatedly without an error"
1273
+ );
1258
1274
  if (!initialDataReceived) {
1259
1275
  rejectInit(
1260
1276
  new Error("stream: max retry count exceeded before receiving data")
@@ -1281,8 +1297,11 @@ async function connectStream(config, callbacks) {
1281
1297
  };
1282
1298
  try {
1283
1299
  lastAttemptTime = Date.now();
1300
+ const token = await config.resolveToken().catch((error) => {
1301
+ throw new TokenResolutionError(error);
1302
+ });
1284
1303
  const headers = {
1285
- Authorization: `Bearer ${sdkKey}`,
1304
+ Authorization: `Bearer ${token}`,
1286
1305
  "User-Agent": `VercelFlagsCore/${version}`,
1287
1306
  "X-Retry-Attempt": String(retryCount)
1288
1307
  };
@@ -1388,8 +1407,13 @@ async function connectStream(config, callbacks) {
1388
1407
  if (abortController.signal.aborted) {
1389
1408
  break;
1390
1409
  }
1410
+ if (error instanceof TokenResolutionError && !initialDataReceived) {
1411
+ rejectInit(error.cause);
1412
+ abortController.abort();
1413
+ break;
1414
+ }
1391
1415
  if (!connectionAbort.signal.aborted) {
1392
- console.error("@vercel/flags-core: Stream error", error);
1416
+ lastError = error;
1393
1417
  }
1394
1418
  onDisconnect?.();
1395
1419
  retryCount++;
@@ -1439,7 +1463,7 @@ var StreamSource = class extends TypedEmitter {
1439
1463
  const promise = connectStream(
1440
1464
  {
1441
1465
  host: this.options.host,
1442
- sdkKey: this.options.sdkKey,
1466
+ resolveToken: () => this.options.auth.resolveToken(),
1443
1467
  abortController,
1444
1468
  fetch: this.options.fetch,
1445
1469
  revision: this.revision
@@ -1526,11 +1550,6 @@ var Controller = class {
1526
1550
  // Suppresses usage tracking when the SDK key is unauthorized
1527
1551
  unauthorized = false;
1528
1552
  constructor(options) {
1529
- if (!options.sdkKey || typeof options.sdkKey !== "string" || !options.sdkKey.startsWith("vf_")) {
1530
- throw new Error(
1531
- '@vercel/flags-core: SDK key must be a string starting with "vf_"'
1532
- );
1533
- }
1534
1553
  this.options = normalizeOptions(options);
1535
1554
  this.streamSource = new StreamSource(
1536
1555
  this.options,
@@ -1538,7 +1557,7 @@ var Controller = class {
1538
1557
  );
1539
1558
  this.pollingSource = new PollingSource(this.options);
1540
1559
  this.bundledSource = new BundledSource({
1541
- sdkKey: this.options.sdkKey,
1560
+ auth: this.options.auth,
1542
1561
  readBundledDefinitions
1543
1562
  });
1544
1563
  this.wireSourceEvents();
@@ -1738,7 +1757,7 @@ var Controller = class {
1738
1757
  try {
1739
1758
  const fetched = await fetchDatafile({
1740
1759
  host: this.options.host,
1741
- sdkKey: this.options.sdkKey,
1760
+ auth: this.options.auth,
1742
1761
  fetch: this.options.fetch
1743
1762
  });
1744
1763
  this.data = tagData(fetched, "fetched");
@@ -1829,7 +1848,7 @@ var Controller = class {
1829
1848
  clearTimeout(timeoutId);
1830
1849
  if (result === "timeout") {
1831
1850
  console.warn(
1832
- "@vercel/flags-core: Stream initialization timeout, falling back"
1851
+ "@vercel/flags-core: Stream initialization timeout, falling back while continuing to connect in the background"
1833
1852
  );
1834
1853
  void this.streamSource.start().catch(() => {
1835
1854
  });
@@ -1879,7 +1898,7 @@ var Controller = class {
1879
1898
  clearTimeout(timeoutId);
1880
1899
  if (result === "timeout") {
1881
1900
  console.warn(
1882
- "@vercel/flags-core: Polling initialization timeout, falling back"
1901
+ "@vercel/flags-core: Polling initialization timeout, falling back while continuing to poll in the background"
1883
1902
  );
1884
1903
  return false;
1885
1904
  }
@@ -1934,7 +1953,7 @@ var Controller = class {
1934
1953
  try {
1935
1954
  const fetched = await fetchDatafile({
1936
1955
  host: this.options.host,
1937
- sdkKey: this.options.sdkKey,
1956
+ auth: this.options.auth,
1938
1957
  fetch: this.options.fetch
1939
1958
  });
1940
1959
  return tagData(fetched, "fetched");
@@ -1966,7 +1985,7 @@ var Controller = class {
1966
1985
  try {
1967
1986
  const fetched = await fetchDatafile({
1968
1987
  host: this.options.host,
1969
- sdkKey: this.options.sdkKey,
1988
+ auth: this.options.auth,
1970
1989
  fetch: this.options.fetch
1971
1990
  });
1972
1991
  this.data = tagData(fetched, "fetched");
@@ -2018,7 +2037,7 @@ var Controller = class {
2018
2037
  try {
2019
2038
  const fetched = await fetchDatafile({
2020
2039
  host: this.options.host,
2021
- sdkKey: this.options.sdkKey,
2040
+ auth: this.options.auth,
2022
2041
  fetch: this.options.fetch
2023
2042
  });
2024
2043
  this.data = tagData(fetched, "fetched");
@@ -2091,6 +2110,9 @@ var Controller = class {
2091
2110
  }
2092
2111
  };
2093
2112
 
2113
+ // src/controller/auth.ts
2114
+ import { getVercelOidcToken } from "@vercel/oidc";
2115
+
2094
2116
  // src/utils/sdk-keys.ts
2095
2117
  var SDK_KEY_REGEX = /^vf_(?:server|client)_/;
2096
2118
  function parseSdkKeyFromFlagsConnectionString(text) {
@@ -2105,28 +2127,80 @@ function parseSdkKeyFromFlagsConnectionString(text) {
2105
2127
  return null;
2106
2128
  }
2107
2129
 
2130
+ // src/controller/auth.ts
2131
+ async function getOidcToken() {
2132
+ try {
2133
+ return await getVercelOidcToken();
2134
+ } catch {
2135
+ throw new Error(
2136
+ [
2137
+ "@vercel/flags-core: Failed to get OIDC token.",
2138
+ "Are you running in a Vercel Environment where OIDC tokens are available?",
2139
+ "Did you mean to use an SDK Key instead? Use the environment variable FLAGS or pass it directly to the client."
2140
+ ].join(" ")
2141
+ );
2142
+ }
2143
+ }
2144
+ function getProjectIdFromOidcToken(oidcToken) {
2145
+ const tokenParts = oidcToken.split(".");
2146
+ if (tokenParts.length !== 3 || !tokenParts[1]) {
2147
+ throw new Error("@vercel/flags-core: Invalid OIDC token");
2148
+ }
2149
+ const payload = JSON.parse(
2150
+ Buffer.from(tokenParts[1], "base64url").toString("utf8")
2151
+ );
2152
+ if (typeof payload.project_id !== "string" || !payload.project_id) {
2153
+ throw new Error(
2154
+ "@vercel/flags-core: Missing project_id claim in OIDC token"
2155
+ );
2156
+ }
2157
+ return payload.project_id;
2158
+ }
2159
+ var Authentication = class {
2160
+ sdkKey;
2161
+ constructor(sdkKeyOrConnectionString) {
2162
+ if (sdkKeyOrConnectionString !== void 0) {
2163
+ if (typeof sdkKeyOrConnectionString !== "string") {
2164
+ throw new Error(
2165
+ `@vercel/flags-core: Invalid sdkKey. Expected string, got ${typeof sdkKeyOrConnectionString}`
2166
+ );
2167
+ }
2168
+ const parsed = parseSdkKeyFromFlagsConnectionString(
2169
+ sdkKeyOrConnectionString
2170
+ );
2171
+ if (!parsed) {
2172
+ throw new Error("@vercel/flags-core: Missing sdkKey");
2173
+ }
2174
+ this.sdkKey = parsed;
2175
+ }
2176
+ }
2177
+ async resolveToken() {
2178
+ if (this.sdkKey) {
2179
+ return this.sdkKey;
2180
+ }
2181
+ return await getOidcToken();
2182
+ }
2183
+ async resolveBundledDefinitionsLookup() {
2184
+ if (this.sdkKey) {
2185
+ return { type: "sdk-key", sdkKey: this.sdkKey };
2186
+ }
2187
+ const oidcToken = await this.resolveToken();
2188
+ return {
2189
+ type: "project-id",
2190
+ projectId: getProjectIdFromOidcToken(oidcToken)
2191
+ };
2192
+ }
2193
+ };
2194
+
2108
2195
  // src/index.make.ts
2109
2196
  function make(createRawClient) {
2110
2197
  let _defaultFlagsClient = null;
2111
2198
  function createClient2(sdkKeyOrConnectionString, options) {
2112
- if (!sdkKeyOrConnectionString)
2113
- throw new Error("@vercel/flags-core: Missing sdkKey");
2114
- if (typeof sdkKeyOrConnectionString !== "string")
2115
- throw new Error(
2116
- `@vercel/flags-core: Invalid sdkKey. Expected string, got ${typeof sdkKeyOrConnectionString}`
2117
- );
2118
- const sdkKey = parseSdkKeyFromFlagsConnectionString(
2119
- sdkKeyOrConnectionString
2120
- );
2121
- if (!sdkKey) {
2122
- throw new Error(
2123
- "@vercel/flags-core: Missing sdkKey in connection string"
2124
- );
2125
- }
2126
- const controller = new Controller({ sdkKey, ...options });
2199
+ const auth = new Authentication(sdkKeyOrConnectionString);
2200
+ const controller = new Controller({ auth, ...options });
2127
2201
  return createRawClient({
2128
2202
  controller,
2129
- origin: { provider: "vercel", sdkKey }
2203
+ origin: { provider: "vercel", sdkKey: auth.sdkKey }
2130
2204
  });
2131
2205
  }
2132
2206
  function resetDefaultFlagsClient2() {
@@ -2135,14 +2209,7 @@ function make(createRawClient) {
2135
2209
  const flagsClient2 = new Proxy({}, {
2136
2210
  get(_, prop) {
2137
2211
  if (!_defaultFlagsClient) {
2138
- if (!process.env.FLAGS) {
2139
- throw new Error("flags: Missing environment variable FLAGS");
2140
- }
2141
- const sdkKey = parseSdkKeyFromFlagsConnectionString(process.env.FLAGS);
2142
- if (!sdkKey) {
2143
- throw new Error("@vercel/flags-core: Missing sdkKey");
2144
- }
2145
- _defaultFlagsClient = createClient2(sdkKey);
2212
+ _defaultFlagsClient = createClient2(process.env.FLAGS);
2146
2213
  }
2147
2214
  return _defaultFlagsClient[prop];
2148
2215
  }
@@ -2208,4 +2275,4 @@ export {
2208
2275
  resetDefaultFlagsClient,
2209
2276
  createClient
2210
2277
  };
2211
- //# sourceMappingURL=chunk-UICJ4U6H.js.map
2278
+ //# sourceMappingURL=chunk-4B7CLR4J.js.map