@tinycloud/mcp 0.8.7-beta.2 → 0.8.7-beta.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.
package/dist/index.js CHANGED
@@ -485,7 +485,7 @@ function outcomeCode(result) {
485
485
  // package.json
486
486
  var package_default = {
487
487
  name: "@tinycloud/mcp",
488
- version: "0.8.7-beta.2",
488
+ version: "0.8.7-beta.3",
489
489
  description: "TinyCloud delegated operations MCP server",
490
490
  keywords: [
491
491
  "tinycloud",
@@ -805,12 +805,14 @@ function firstString(...values) {
805
805
  import { createHash } from "crypto";
806
806
  import { decodeJwt } from "jose";
807
807
  var ACTIVE_CACHE_MS = 3e4;
808
+ var UNKNOWN_TOLERANCE_MS = 6e4;
808
809
  var DEFAULT_TOKEN_LIFETIME_SECONDS = 300;
809
810
  var DEFAULT_TIMEOUT_MS = 3e3;
810
811
  var MAX_CACHED_TOKENS = 1e4;
811
812
  function createTokenStatusChecker(config) {
812
813
  const doFetch = config.fetch ?? fetch;
813
814
  const clock = config.now ?? Date.now;
815
+ const monotonic = config.monotonicNow ?? (config.now ?? (() => performance.now()));
814
816
  const log = config.log ?? ((message) => void process.stderr.write(`[tinycloud-mcp-http] ${message}
815
817
  `));
816
818
  const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
@@ -853,6 +855,7 @@ function createTokenStatusChecker(config) {
853
855
  }
854
856
  }
855
857
  async function query(key, token) {
858
+ const askedAt = monotonic();
856
859
  const outcome = await introspect(token);
857
860
  if (outcome === "credentials") {
858
861
  if (!credentialsRefused) {
@@ -863,7 +866,7 @@ function createTokenStatusChecker(config) {
863
866
  }
864
867
  if (outcome === "unknown") return outcome;
865
868
  const now = clock();
866
- prune(now);
869
+ prune(now, monotonic());
867
870
  if (outcome === "mismatch") {
868
871
  inactive.set(key, tokenExpiryMs(token, now));
869
872
  confirmedActive.delete(key);
@@ -874,16 +877,18 @@ function createTokenStatusChecker(config) {
874
877
  inactive.set(key, tokenExpiryMs(token, now));
875
878
  confirmedActive.delete(key);
876
879
  } else {
877
- active.set(key, now + ACTIVE_CACHE_MS);
880
+ active.set(key, askedAt + ACTIVE_CACHE_MS);
878
881
  confirmedActive.delete(key);
879
- confirmedActive.set(key, { at: now, expiresAt: tokenExpiryMs(token, now) });
882
+ confirmedActive.set(key, { at: askedAt, expiresAt: tokenExpiryMs(token, now) });
880
883
  }
881
884
  return outcome;
882
885
  }
883
- function prune(now) {
884
- for (const [key, until] of active) if (until <= now) active.delete(key);
886
+ function prune(now, elapsed) {
887
+ for (const [key, until] of active) if (until <= elapsed) active.delete(key);
885
888
  for (const [key, until] of inactive) if (until <= now) inactive.delete(key);
886
- for (const [key, entry] of confirmedActive) if (entry.expiresAt <= now) confirmedActive.delete(key);
889
+ for (const [key, entry] of confirmedActive) {
890
+ if (entry.expiresAt <= now || elapsed - entry.at >= UNKNOWN_TOLERANCE_MS) confirmedActive.delete(key);
891
+ }
887
892
  for (const cache of [active, inactive, confirmedActive]) {
888
893
  while (cache.size >= MAX_CACHED_TOKENS) {
889
894
  const oldest = cache.keys().next().value;
@@ -896,10 +901,11 @@ function createTokenStatusChecker(config) {
896
901
  async tokenStatus(token) {
897
902
  const key = createHash("sha256").update(token).digest("hex");
898
903
  const now = clock();
904
+ const elapsed = monotonic();
899
905
  const inactiveUntil = inactive.get(key);
900
906
  if (inactiveUntil !== void 0 && inactiveUntil > now) return "inactive";
901
907
  const activeUntil = active.get(key);
902
- if (activeUntil !== void 0 && activeUntil > now) return "active";
908
+ if (activeUntil !== void 0 && activeUntil > elapsed) return "active";
903
909
  const pending = inFlight.get(key);
904
910
  if (pending !== void 0) return pending;
905
911
  const asking = query(key, token).finally(() => inFlight.delete(key));
@@ -910,8 +916,7 @@ function createTokenStatusChecker(config) {
910
916
  if (credentialsRefused) return false;
911
917
  const confirmed = confirmedActive.get(createHash("sha256").update(token).digest("hex"));
912
918
  if (confirmed === void 0) return false;
913
- const now = clock();
914
- return confirmed.expiresAt > now && now - confirmed.at < tokenLifetimeSeconds(token) * 1e3;
919
+ return confirmed.expiresAt > clock() && monotonic() - confirmed.at < UNKNOWN_TOLERANCE_MS;
915
920
  }
916
921
  };
917
922
  }
@@ -933,10 +938,6 @@ function tokenExpiryMs(token, now) {
933
938
  const exp = claims(token).exp;
934
939
  return typeof exp === "number" ? exp * 1e3 : now + DEFAULT_TOKEN_LIFETIME_SECONDS * 1e3;
935
940
  }
936
- function tokenLifetimeSeconds(token) {
937
- const { exp, iat } = claims(token);
938
- return typeof exp === "number" && typeof iat === "number" && exp > iat ? exp - iat : DEFAULT_TOKEN_LIFETIME_SECONDS;
939
- }
940
941
  function claims(token) {
941
942
  try {
942
943
  return decodeJwt(token);