@ordewell/core 0.13.4 → 0.14.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.
package/dist/index.mjs CHANGED
@@ -2529,13 +2529,12 @@ function compareVersions2(a, b) {
2529
2529
  return 0;
2530
2530
  }
2531
2531
  var isNewer = (latest, version) => (compareVersions2(latest, version) ?? 0) > 0;
2532
- async function readAnswer(response, version) {
2533
- if (response.status === 204) return null;
2532
+ async function readAnswer(response) {
2534
2533
  if (response.status !== 200) return void 0;
2535
2534
  const body = JSON.parse(await response.text());
2536
2535
  if (!body || typeof body !== "object") return void 0;
2537
2536
  const { latest } = body;
2538
- return typeof latest === "string" && isNewer(latest, version) ? latest : null;
2537
+ return typeof latest === "string" && SEMVER.test(latest) ? latest : void 0;
2539
2538
  }
2540
2539
  var truthy = (value) => /^(1|true|yes)$/i.test(value?.trim() ?? "");
2541
2540
  var OFF = { settled: async () => {
@@ -2549,6 +2548,7 @@ function startVersionCheck(options) {
2549
2548
  const file = join4(options.stateDir ?? globalDataDir(), STATE_FILE);
2550
2549
  let inFlight = Promise.resolve();
2551
2550
  let pinging = false;
2551
+ let stopped2 = false;
2552
2552
  let named3;
2553
2553
  const lessThanADayAgo = (at) => {
2554
2554
  if (!at) return false;
@@ -2556,7 +2556,7 @@ function startVersionCheck(options) {
2556
2556
  return since >= 0 && since < DAY_MS;
2557
2557
  };
2558
2558
  const showUpdate = (latest) => {
2559
- if (!options.onUpdate || latest === named3) return;
2559
+ if (stopped2 || !options.onUpdate || latest === named3 || !isNewer(latest, options.version)) return;
2560
2560
  if (options.updateLine === "daily") {
2561
2561
  const state2 = readState(file);
2562
2562
  if (lessThanADayAgo(state2.updateShownAt) || !writeState(file, { ...state2, updateShownAt: new Date(now()).toISOString() })) return;
@@ -2570,27 +2570,28 @@ function startVersionCheck(options) {
2570
2570
  const ping = async (version, installId) => {
2571
2571
  const query = new URLSearchParams({ v: version, id: installId });
2572
2572
  const abort = new AbortController();
2573
- const timer2 = setTimeout(() => abort.abort(), TIMEOUT_MS);
2574
- timer2.unref?.();
2573
+ const timer = setTimeout(() => abort.abort(), TIMEOUT_MS);
2574
+ timer.unref?.();
2575
2575
  try {
2576
2576
  const response = await fetchImpl(`${VERSION_CHECK_URL}?${query}`, {
2577
2577
  method: "GET",
2578
2578
  signal: abort.signal,
2579
2579
  redirect: "manual"
2580
2580
  });
2581
- const latest = await readAnswer(response, version);
2581
+ const latest = await readAnswer(response);
2582
2582
  if (latest === void 0) return;
2583
2583
  const state2 = readState(file);
2584
- delete state2.latest;
2585
- delete state2.latestAt;
2586
- writeState(file, latest ? { ...state2, latest, latestAt: new Date(now()).toISOString() } : state2);
2587
- if (latest) showUpdate(latest);
2584
+ if (state2.latest && isNewer(state2.latest, latest)) return;
2585
+ writeState(file, { ...state2, latest, latestAt: new Date(now()).toISOString() });
2586
+ showUpdate(latest);
2588
2587
  } finally {
2589
- clearTimeout(timer2);
2588
+ clearTimeout(timer);
2590
2589
  }
2591
2590
  };
2592
- const check2 = () => {
2593
- if (pinging) return;
2591
+ const check2 = (mayPing) => {
2592
+ const remembered = readState(file).latest;
2593
+ if (remembered) showUpdate(remembered);
2594
+ if (!mayPing || pinging) return;
2594
2595
  const state2 = readState(file);
2595
2596
  if (!state2.installId) return;
2596
2597
  if (lessThanADayAgo(state2.lastCheckAt)) return;
@@ -2601,6 +2602,17 @@ function startVersionCheck(options) {
2601
2602
  pinging = false;
2602
2603
  });
2603
2604
  };
2605
+ const recheckEveryHour = (mayPing) => {
2606
+ const timer = setInterval(() => check2(mayPing), RECHECK_MS);
2607
+ timer.unref?.();
2608
+ return {
2609
+ settled: () => inFlight,
2610
+ stop: () => {
2611
+ stopped2 = true;
2612
+ clearInterval(timer);
2613
+ }
2614
+ };
2615
+ };
2604
2616
  const state = rotateInstallId(file, readState(file), now());
2605
2617
  if (!state.noticeShownAt || !state.installId) {
2606
2618
  const installId = state.installId ?? randomUUID();
@@ -2610,17 +2622,15 @@ function startVersionCheck(options) {
2610
2622
  installIdAt: state.installId && state.installIdAt ? state.installIdAt : new Date(now()).toISOString(),
2611
2623
  noticeShownAt: new Date(now()).toISOString()
2612
2624
  });
2625
+ if (!recorded3) return OFF;
2613
2626
  try {
2614
- if (recorded3) options.onNotice(VERSION_CHECK_NOTICE);
2627
+ options.onNotice(VERSION_CHECK_NOTICE);
2615
2628
  } catch {
2616
2629
  }
2617
- return OFF;
2630
+ return recheckEveryHour(false);
2618
2631
  }
2619
- if (state.latest && isNewer(state.latest, options.version)) showUpdate(state.latest);
2620
- check2();
2621
- const timer = setInterval(check2, RECHECK_MS);
2622
- timer.unref?.();
2623
- return { settled: () => inFlight, stop: () => clearInterval(timer) };
2632
+ check2(true);
2633
+ return recheckEveryHour(true);
2624
2634
  }
2625
2635
 
2626
2636
  // src/services/effectAllowlist.ts