@hamedb89/localghost 0.4.1 → 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/vite.js CHANGED
@@ -306,6 +306,9 @@ function validRegistry(value) {
306
306
  function pruneRegistry(registry, now, isRunning) {
307
307
  registry.leases = registry.leases.filter((lease) => lease.expiresAt > now && isRunning(lease.pid));
308
308
  }
309
+ function isTestSessionKey(instanceKey) {
310
+ return instanceKey.startsWith("test:");
311
+ }
309
312
  async function readJson(path) {
310
313
  try {
311
314
  return JSON.parse(await readFile(path, "utf8"));
@@ -402,6 +405,37 @@ function createLocalghostRegistry(options = {}) {
402
405
  await releaseLock();
403
406
  }
404
407
  },
408
+ async pruneTestSessions() {
409
+ const releaseLock = await lock();
410
+ try {
411
+ const registry = await readRegistry();
412
+ const staleTestKeys = new Set(
413
+ registry.leases.filter((lease) => isTestSessionKey(lease.instanceKey) && (lease.expiresAt <= now() || !isRunning(lease.pid))).map((lease) => leaseKey(lease.projectCwd, lease.instanceKey))
414
+ );
415
+ const beforeLeases = registry.leases.length;
416
+ registry.leases = registry.leases.filter((lease) => !staleTestKeys.has(leaseKey(lease.projectCwd, lease.instanceKey)));
417
+ const activeKeys = new Set(registry.leases.map((lease) => leaseKey(lease.projectCwd, lease.instanceKey)));
418
+ const beforeAllocations = registry.allocations.length;
419
+ registry.allocations = registry.allocations.filter(
420
+ (allocation) => !isTestSessionKey(allocation.instanceKey) || activeKeys.has(leaseKey(allocation.projectCwd, allocation.instanceKey))
421
+ );
422
+ await writeRegistry(registry);
423
+ return {
424
+ removedLeases: beforeLeases - registry.leases.length,
425
+ removedAllocations: beforeAllocations - registry.allocations.length
426
+ };
427
+ } finally {
428
+ await releaseLock();
429
+ }
430
+ },
431
+ async reset() {
432
+ const releaseLock = await lock();
433
+ try {
434
+ await writeRegistry({ version: 1, allocations: [], leases: [] });
435
+ } finally {
436
+ await releaseLock();
437
+ }
438
+ },
405
439
  async acquirePort(acquireOptions) {
406
440
  const projectCwd = canonicalizeLocalghostProjectCwd(acquireOptions.projectCwd ?? cwd);
407
441
  if (!acquireOptions.instanceKey) throw new Error("instanceKey is required");
@@ -439,6 +473,18 @@ function createLocalghostRegistry(options = {}) {
439
473
  return lease;
440
474
  });
441
475
  },
476
+ async renewPort(renewOptions) {
477
+ const projectCwd = canonicalizeLocalghostProjectCwd(renewOptions.projectCwd ?? cwd);
478
+ return withLock(async (registry) => {
479
+ const lease = registry.leases.find(
480
+ (candidate) => candidate.projectCwd === projectCwd && candidate.instanceKey === renewOptions.instanceKey && candidate.ownerToken === ownerToken
481
+ );
482
+ if (!lease || lease.expiresAt <= now() || !isRunning(lease.pid)) return void 0;
483
+ lease.expiresAt = now() + (renewOptions.leaseTtlMs ?? 30 * 60 * 1e3);
484
+ await writeRegistry(registry);
485
+ return lease;
486
+ });
487
+ },
442
488
  async releasePort(releaseOptions) {
443
489
  const projectCwd = canonicalizeLocalghostProjectCwd(releaseOptions.projectCwd ?? cwd);
444
490
  return withLock(async (registry) => {