@vercel/flags-core 1.1.0 → 1.2.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.
@@ -376,16 +376,18 @@ function findWeightedIndex(weights, value, maxValue) {
376
376
  }
377
377
 
378
378
  // package.json
379
- var version = "1.1.0";
379
+ var version = "1.2.0";
380
380
 
381
381
  // src/lib/report-value.ts
382
382
  function internalReportValue(key, value, data) {
383
383
  const symbol = /* @__PURE__ */ Symbol.for("@vercel/request-context");
384
384
  const ctx = _optionalChain([Reflect, 'access', _14 => _14.get, 'call', _15 => _15(globalThis, symbol), 'optionalAccess', _16 => _16.get, 'call', _17 => _17()]);
385
- _optionalChain([ctx, 'optionalAccess', _18 => _18.flags, 'optionalAccess', _19 => _19.reportValue, 'call', _20 => _20(key, value, {
385
+ const reportFlagValue = _optionalChain([ctx, 'optionalAccess', _18 => _18.flags, 'optionalAccess', _19 => _19.reportValue]);
386
+ if (typeof reportFlagValue !== "function") return;
387
+ reportFlagValue.call(ctx.flags, key, value, {
386
388
  sdkVersion: version,
387
389
  ...data
388
- })]);
390
+ });
389
391
  }
390
392
 
391
393
  // src/controller-fns.ts
@@ -526,7 +528,7 @@ function createCreateRawClient(fns) {
526
528
  },
527
529
  getDatafile: async () => {
528
530
  const instance = controllerInstanceMap.get(id);
529
- if (_optionalChain([instance, 'optionalAccess', _21 => _21.initPromise])) {
531
+ if (_optionalChain([instance, 'optionalAccess', _20 => _20.initPromise])) {
530
532
  try {
531
533
  await instance.initPromise;
532
534
  } catch (e) {
@@ -539,7 +541,7 @@ function createCreateRawClient(fns) {
539
541
  },
540
542
  evaluate: async (flagKey, defaultValue, entities) => {
541
543
  const instance = controllerInstanceMap.get(id);
542
- if (!_optionalChain([instance, 'optionalAccess', _22 => _22.initialized])) {
544
+ if (!_optionalChain([instance, 'optionalAccess', _21 => _21.initialized])) {
543
545
  try {
544
546
  await api.initialize();
545
547
  } catch (e2) {
@@ -584,6 +586,9 @@ async function readBundledDefinitions(sdkKey) {
584
586
  }
585
587
  return { definitions: null, state: "unexpected-error", error };
586
588
  }
589
+ if (typeof get !== "function") {
590
+ return { definitions: null, state: "missing-file" };
591
+ }
587
592
  const entry = get(sdkKey);
588
593
  if (entry) return { definitions: entry, state: "ok" };
589
594
  try {
@@ -599,7 +604,7 @@ async function readBundledDefinitions(sdkKey) {
599
604
  // src/utils/usage-tracker.ts
600
605
  var _functions = require('@vercel/functions');
601
606
  var RESOLVED_VOID = Promise.resolve();
602
- var isDebugMode = _optionalChain([process, 'access', _23 => _23.env, 'access', _24 => _24.DEBUG, 'optionalAccess', _25 => _25.includes, 'call', _26 => _26("@vercel/flags-core")]);
607
+ var isDebugMode = _optionalChain([process, 'access', _22 => _22.env, 'access', _23 => _23.DEBUG, 'optionalAccess', _24 => _24.includes, 'call', _25 => _25("@vercel/flags-core")]);
603
608
  var debugLog = (...args) => {
604
609
  if (!isDebugMode) return;
605
610
  console.log(...args);
@@ -611,7 +616,7 @@ var SYMBOL_FOR_REQ_CONTEXT = /* @__PURE__ */ Symbol.for("@vercel/request-context
611
616
  var fromSymbol = globalThis;
612
617
  function getRequestContext() {
613
618
  try {
614
- const ctx = _optionalChain([fromSymbol, 'access', _27 => _27[SYMBOL_FOR_REQ_CONTEXT], 'optionalAccess', _28 => _28.get, 'optionalCall', _29 => _29()]);
619
+ const ctx = _optionalChain([fromSymbol, 'access', _26 => _26[SYMBOL_FOR_REQ_CONTEXT], 'optionalAccess', _27 => _27.get, 'optionalCall', _28 => _28()]);
615
620
  if (ctx && Object.hasOwn(ctx, "headers")) {
616
621
  return {
617
622
  ctx,
@@ -641,7 +646,7 @@ var UsageTracker = (_class = class {
641
646
  */
642
647
  flush() {
643
648
  if (this.batcher.pending) {
644
- _optionalChain([this, 'access', _30 => _30.batcher, 'access', _31 => _31.resolveWait, 'optionalCall', _32 => _32()]);
649
+ _optionalChain([this, 'access', _29 => _29.batcher, 'access', _30 => _30.resolveWait, 'optionalCall', _31 => _31()]);
645
650
  return this.batcher.pending;
646
651
  }
647
652
  if (this.batcher.events.length > 0) {
@@ -655,10 +660,9 @@ var UsageTracker = (_class = class {
655
660
  trackRead(options) {
656
661
  try {
657
662
  const { ctx, headers } = getRequestContext();
658
- if (ctx) {
659
- if (this.trackedRequests.has(ctx)) return;
660
- this.trackedRequests.add(ctx);
661
- }
663
+ if (!ctx) return;
664
+ if (this.trackedRequests.has(ctx)) return;
665
+ this.trackedRequests.add(ctx);
662
666
  const event = {
663
667
  type: "FLAGS_CONFIG_READ",
664
668
  ts: Date.now(),
@@ -728,7 +732,7 @@ var UsageTracker = (_class = class {
728
732
  this.batcher.pending = pending;
729
733
  }
730
734
  if (this.batcher.events.length >= MAX_BATCH_SIZE) {
731
- _optionalChain([this, 'access', _33 => _33.batcher, 'access', _34 => _34.resolveWait, 'optionalCall', _35 => _35()]);
735
+ _optionalChain([this, 'access', _32 => _32.batcher, 'access', _33 => _33.resolveWait, 'optionalCall', _34 => _34()]);
732
736
  }
733
737
  }
734
738
  async flushEvents() {
@@ -746,6 +750,7 @@ var UsageTracker = (_class = class {
746
750
  "Content-Type": "application/json",
747
751
  Authorization: `Bearer ${this.options.sdkKey}`,
748
752
  "User-Agent": `VercelFlagsCore/${version}`,
753
+ ...process.env.VERCEL_ENV ? { "X-Vercel-Env": process.env.VERCEL_ENV } : null,
749
754
  ...isDebugMode ? { "x-vercel-debug-ingest": "1" } : null
750
755
  },
751
756
  body: JSON.stringify(eventsToSend)
@@ -832,7 +837,7 @@ var BundledSource = class {
832
837
  */
833
838
  async tryLoad() {
834
839
  const result = await this.getResult();
835
- if (_optionalChain([result, 'optionalAccess', _36 => _36.state]) === "ok" && result.definitions) {
840
+ if (_optionalChain([result, 'optionalAccess', _35 => _35.state]) === "ok" && result.definitions) {
836
841
  return result.definitions;
837
842
  }
838
843
  return void 0;
@@ -865,19 +870,20 @@ async function fetchDatafile(options) {
865
870
  const res = await options.fetch(`${options.host}/v1/datafile`, {
866
871
  headers: {
867
872
  Authorization: `Bearer ${options.sdkKey}`,
868
- "User-Agent": `VercelFlagsCore/${version}`
873
+ "User-Agent": `VercelFlagsCore/${version}`,
874
+ ...process.env.VERCEL_ENV ? { "X-Vercel-Env": process.env.VERCEL_ENV } : null
869
875
  },
870
876
  signal: controller.signal
871
877
  });
872
878
  clearTimeout(timeoutId);
873
- _optionalChain([options, 'access', _37 => _37.signal, 'optionalAccess', _38 => _38.removeEventListener, 'call', _39 => _39("abort", onExternalAbort)]);
879
+ _optionalChain([options, 'access', _36 => _36.signal, 'optionalAccess', _37 => _37.removeEventListener, 'call', _38 => _38("abort", onExternalAbort)]);
874
880
  if (!res.ok) {
875
881
  throw new Error(`Failed to fetch data: ${res.statusText}`);
876
882
  }
877
883
  return res.json();
878
884
  } catch (error) {
879
885
  clearTimeout(timeoutId);
880
- _optionalChain([options, 'access', _40 => _40.signal, 'optionalAccess', _41 => _41.removeEventListener, 'call', _42 => _42("abort", onExternalAbort)]);
886
+ _optionalChain([options, 'access', _39 => _39.signal, 'optionalAccess', _40 => _40.removeEventListener, 'call', _41 => _41("abort", onExternalAbort)]);
881
887
  throw error instanceof Error ? error : new Error("Unknown fetch error");
882
888
  }
883
889
  }
@@ -942,7 +948,7 @@ var TypedEmitter = (_class2 = class {constructor() { _class2.prototype.__init4.c
942
948
  set.add(handler);
943
949
  }
944
950
  off(event, handler) {
945
- _optionalChain([this, 'access', _43 => _43.handlers, 'access', _44 => _44.get, 'call', _45 => _45(event), 'optionalAccess', _46 => _46.delete, 'call', _47 => _47(handler)]);
951
+ _optionalChain([this, 'access', _42 => _42.handlers, 'access', _43 => _43.get, 'call', _44 => _44(event), 'optionalAccess', _45 => _45.delete, 'call', _46 => _46(handler)]);
946
952
  }
947
953
  emit(event, ...args) {
948
954
  const set = this.handlers.get(event);
@@ -968,11 +974,11 @@ var PollingSource = class extends TypedEmitter {
968
974
  * Emits 'data' on success, 'error' on failure.
969
975
  */
970
976
  async poll() {
971
- if (_optionalChain([this, 'access', _48 => _48.abortController, 'optionalAccess', _49 => _49.signal, 'access', _50 => _50.aborted])) return;
977
+ if (_optionalChain([this, 'access', _47 => _47.abortController, 'optionalAccess', _48 => _48.signal, 'access', _49 => _49.aborted])) return;
972
978
  try {
973
979
  const data = await fetchDatafile({
974
980
  ...this.config,
975
- signal: _optionalChain([this, 'access', _51 => _51.abortController, 'optionalAccess', _52 => _52.signal])
981
+ signal: _optionalChain([this, 'access', _50 => _50.abortController, 'optionalAccess', _51 => _51.signal])
976
982
  });
977
983
  this.emit("data", data);
978
984
  } catch (error) {
@@ -1001,7 +1007,7 @@ var PollingSource = class extends TypedEmitter {
1001
1007
  clearInterval(this.intervalId);
1002
1008
  this.intervalId = void 0;
1003
1009
  }
1004
- _optionalChain([this, 'access', _53 => _53.abortController, 'optionalAccess', _54 => _54.abort, 'call', _55 => _55()]);
1010
+ _optionalChain([this, 'access', _52 => _52.abortController, 'optionalAccess', _53 => _53.abort, 'call', _54 => _54()]);
1005
1011
  this.abortController = void 0;
1006
1012
  }
1007
1013
  };
@@ -1070,7 +1076,7 @@ async function connectStream(config, callbacks) {
1070
1076
  if (pingTimeoutId !== void 0) clearTimeout(pingTimeoutId);
1071
1077
  if (!initialDataReceived) return;
1072
1078
  pingTimeoutId = setTimeout(() => {
1073
- _optionalChain([responseBody, 'optionalAccess', _56 => _56.cancel, 'call', _57 => _57(), 'access', _58 => _58.catch, 'call', _59 => _59(() => {
1079
+ _optionalChain([responseBody, 'optionalAccess', _55 => _55.cancel, 'call', _56 => _56(), 'access', _57 => _57.catch, 'call', _58 => _58(() => {
1074
1080
  })]);
1075
1081
  connectionAbort.abort();
1076
1082
  }, PING_TIMEOUT_MS);
@@ -1082,7 +1088,11 @@ async function connectStream(config, callbacks) {
1082
1088
  "User-Agent": `VercelFlagsCore/${version}`,
1083
1089
  "X-Retry-Attempt": String(retryCount)
1084
1090
  };
1085
- const revision = _optionalChain([config, 'access', _60 => _60.revision, 'optionalCall', _61 => _61()]);
1091
+ const vercelEnv = process.env.VERCEL_ENV;
1092
+ if (vercelEnv) {
1093
+ headers["X-Vercel-Env"] = vercelEnv;
1094
+ }
1095
+ const revision = _optionalChain([config, 'access', _59 => _59.revision, 'optionalCall', _60 => _60()]);
1086
1096
  if (revision !== void 0) {
1087
1097
  headers["X-Revision"] = String(revision);
1088
1098
  }
@@ -1144,7 +1154,7 @@ async function connectStream(config, callbacks) {
1144
1154
  resetPingTimeout();
1145
1155
  }
1146
1156
  if (message.type === "primed") {
1147
- _optionalChain([onPrimed, 'optionalCall', _62 => _62(message)]);
1157
+ _optionalChain([onPrimed, 'optionalCall', _61 => _61(message)]);
1148
1158
  retryCount = 0;
1149
1159
  if (!initialDataReceived) {
1150
1160
  initialDataReceived = true;
@@ -1167,7 +1177,7 @@ async function connectStream(config, callbacks) {
1167
1177
  clearTimeout(pingTimeoutId);
1168
1178
  abortController.signal.removeEventListener("abort", onMainAbort);
1169
1179
  if (!abortController.signal.aborted) {
1170
- _optionalChain([onDisconnect, 'optionalCall', _63 => _63()]);
1180
+ _optionalChain([onDisconnect, 'optionalCall', _62 => _62()]);
1171
1181
  retryCount++;
1172
1182
  const elapsed = Date.now() - lastAttemptTime;
1173
1183
  const minGap = Math.max(0, BASE_RETRY_DELAY_MS - elapsed);
@@ -1183,7 +1193,7 @@ async function connectStream(config, callbacks) {
1183
1193
  if (!connectionAbort.signal.aborted) {
1184
1194
  console.error("@vercel/flags-core: Stream error", error);
1185
1195
  }
1186
- _optionalChain([onDisconnect, 'optionalCall', _64 => _64()]);
1196
+ _optionalChain([onDisconnect, 'optionalCall', _63 => _63()]);
1187
1197
  retryCount++;
1188
1198
  const elapsed = Date.now() - lastAttemptTime;
1189
1199
  const minGap = Math.max(0, BASE_RETRY_DELAY_MS - elapsed);
@@ -1262,7 +1272,7 @@ var StreamSource = class extends TypedEmitter {
1262
1272
  * Stop the stream connection.
1263
1273
  */
1264
1274
  stop() {
1265
- _optionalChain([this, 'access', _65 => _65.abortController, 'optionalAccess', _66 => _66.abort, 'call', _67 => _67()]);
1275
+ _optionalChain([this, 'access', _64 => _64.abortController, 'optionalAccess', _65 => _65.abort, 'call', _66 => _66()]);
1266
1276
  this.abortController = void 0;
1267
1277
  this.promise = void 0;
1268
1278
  }
@@ -1321,7 +1331,7 @@ var Controller = (_class3 = class {
1321
1331
  this.options = normalizeOptions(options);
1322
1332
  this.streamSource = new StreamSource(
1323
1333
  this.options,
1324
- () => _optionalChain([this, 'access', _68 => _68.data, 'optionalAccess', _69 => _69.revision])
1334
+ () => _optionalChain([this, 'access', _67 => _67.data, 'optionalAccess', _68 => _68.revision])
1325
1335
  );
1326
1336
  this.pollingSource = new PollingSource(this.options);
1327
1337
  this.bundledSource = new BundledSource({
@@ -1854,11 +1864,11 @@ var Controller = (_class3 = class {
1854
1864
  duration: Date.now() - startTime,
1855
1865
  mode: mode === "streaming" ? "stream" : mode === "polling" ? "poll" : mode
1856
1866
  };
1857
- const configUpdatedAt = _optionalChain([this, 'access', _70 => _70.data, 'optionalAccess', _71 => _71.configUpdatedAt]);
1867
+ const configUpdatedAt = _optionalChain([this, 'access', _69 => _69.data, 'optionalAccess', _70 => _70.configUpdatedAt]);
1858
1868
  if (typeof configUpdatedAt === "number") {
1859
1869
  trackOptions.configUpdatedAt = configUpdatedAt;
1860
1870
  }
1861
- const revision = _optionalChain([this, 'access', _72 => _72.data, 'optionalAccess', _73 => _73.revision]);
1871
+ const revision = _optionalChain([this, 'access', _71 => _71.data, 'optionalAccess', _72 => _72.revision]);
1862
1872
  if (typeof revision === "number") {
1863
1873
  trackOptions.revision = revision;
1864
1874
  }
@@ -1979,4 +1989,4 @@ var { flagsClient, resetDefaultFlagsClient, createClient } = make(
1979
1989
 
1980
1990
 
1981
1991
  exports.ResolutionReason = ResolutionReason; exports.evaluate = evaluate; exports.FallbackNotFoundError = FallbackNotFoundError; exports.FallbackEntryNotFoundError = FallbackEntryNotFoundError; exports.Controller = Controller; exports.flagsClient = flagsClient; exports.resetDefaultFlagsClient = resetDefaultFlagsClient; exports.createClient = createClient;
1982
- //# sourceMappingURL=chunk-LBP3OTYA.cjs.map
1992
+ //# sourceMappingURL=chunk-WN6NH2JT.cjs.map