@hamedb89/localghost 0.5.0 → 0.6.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/dist/index.d.ts CHANGED
@@ -607,6 +607,11 @@ type LocalghostRegistry = {
607
607
  prune(): Promise<{
608
608
  removedLeases: number;
609
609
  }>;
610
+ pruneTestSessions(): Promise<{
611
+ removedLeases: number;
612
+ removedAllocations: number;
613
+ }>;
614
+ reset(): Promise<void>;
610
615
  };
611
616
  declare function getLocalghostRegistryRoot(env?: NodeJS.ProcessEnv): string;
612
617
  declare function canonicalizeLocalghostProjectCwd(cwd?: string): string;
@@ -711,7 +716,7 @@ type CreateVercelGhostTunnelHandlerOptions = {
711
716
  declare function createVercelGhostTunnelHandler(options: CreateVercelGhostTunnelHandlerOptions): (request: VercelGhostTunnelRequestLike, response: VercelGhostTunnelResponseLike) => Promise<void>;
712
717
 
713
718
  declare const LOCALGHOST_PACKAGE_NAME = "@hamedb89/localghost";
714
- declare const LOCALGHOST_VERSION = "0.5.0";
719
+ declare const LOCALGHOST_VERSION = "0.6.1";
715
720
  declare const UPDATE_CHECK_CACHE_TTL_MS: number;
716
721
  declare const UPDATE_CHECK_NOTIFY_TTL_MS: number;
717
722
  declare const UPDATE_CHECK_TIMEOUT_MS = 900;
package/dist/index.js CHANGED
@@ -1755,6 +1755,9 @@ function validRegistry(value) {
1755
1755
  function pruneRegistry(registry, now, isRunning) {
1756
1756
  registry.leases = registry.leases.filter((lease) => lease.expiresAt > now && isRunning(lease.pid));
1757
1757
  }
1758
+ function isTestSessionKey(instanceKey) {
1759
+ return instanceKey.startsWith("test:");
1760
+ }
1758
1761
  async function readJson(path) {
1759
1762
  try {
1760
1763
  return JSON.parse(await readFile(path, "utf8"));
@@ -1851,6 +1854,37 @@ function createLocalghostRegistry(options = {}) {
1851
1854
  await releaseLock();
1852
1855
  }
1853
1856
  },
1857
+ async pruneTestSessions() {
1858
+ const releaseLock = await lock();
1859
+ try {
1860
+ const registry = await readRegistry();
1861
+ const staleTestKeys = new Set(
1862
+ registry.leases.filter((lease) => isTestSessionKey(lease.instanceKey) && (lease.expiresAt <= now() || !isRunning(lease.pid))).map((lease) => leaseKey(lease.projectCwd, lease.instanceKey))
1863
+ );
1864
+ const beforeLeases = registry.leases.length;
1865
+ registry.leases = registry.leases.filter((lease) => !staleTestKeys.has(leaseKey(lease.projectCwd, lease.instanceKey)));
1866
+ const activeKeys = new Set(registry.leases.map((lease) => leaseKey(lease.projectCwd, lease.instanceKey)));
1867
+ const beforeAllocations = registry.allocations.length;
1868
+ registry.allocations = registry.allocations.filter(
1869
+ (allocation) => !isTestSessionKey(allocation.instanceKey) || activeKeys.has(leaseKey(allocation.projectCwd, allocation.instanceKey))
1870
+ );
1871
+ await writeRegistry(registry);
1872
+ return {
1873
+ removedLeases: beforeLeases - registry.leases.length,
1874
+ removedAllocations: beforeAllocations - registry.allocations.length
1875
+ };
1876
+ } finally {
1877
+ await releaseLock();
1878
+ }
1879
+ },
1880
+ async reset() {
1881
+ const releaseLock = await lock();
1882
+ try {
1883
+ await writeRegistry({ version: 1, allocations: [], leases: [] });
1884
+ } finally {
1885
+ await releaseLock();
1886
+ }
1887
+ },
1854
1888
  async acquirePort(acquireOptions) {
1855
1889
  const projectCwd = canonicalizeLocalghostProjectCwd(acquireOptions.projectCwd ?? cwd);
1856
1890
  if (!acquireOptions.instanceKey) throw new Error("instanceKey is required");
@@ -2628,7 +2662,7 @@ async function createLocalghostTestSession(options) {
2628
2662
 
2629
2663
  // src/process.ts
2630
2664
  function signalManagedProcessPid(pid, signal, killProcess = (value, processSignal) => process.kill(value, processSignal)) {
2631
- if (typeof pid !== "number" || !Number.isInteger(pid) || pid < 1) return false;
2665
+ if (typeof pid !== "number" || !Number.isInteger(pid) || pid <= 1 || pid === process.pid) return false;
2632
2666
  try {
2633
2667
  killProcess(process.platform === "win32" ? pid : -pid, signal);
2634
2668
  return true;
@@ -2964,7 +2998,7 @@ import { existsSync as existsSync7, mkdirSync as mkdirSync3, readFileSync as rea
2964
2998
  import { homedir as homedir3 } from "os";
2965
2999
  import { dirname as dirname5, join as join10 } from "path";
2966
3000
  var LOCALGHOST_PACKAGE_NAME = "@hamedb89/localghost";
2967
- var LOCALGHOST_VERSION = "0.5.0";
3001
+ var LOCALGHOST_VERSION = "0.6.1";
2968
3002
  var UPDATE_CHECK_CACHE_TTL_MS = 24 * 60 * 60 * 1e3;
2969
3003
  var UPDATE_CHECK_NOTIFY_TTL_MS = 24 * 60 * 60 * 1e3;
2970
3004
  var UPDATE_CHECK_TIMEOUT_MS = 900;