dsh-lan-guard 0.1.0 → 0.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.
package/lib/index.js CHANGED
@@ -1500,6 +1500,130 @@ function startMdns(options) {
1500
1500
  }
1501
1501
  } };
1502
1502
  }
1503
+ /** Registry document that carries the `latest` tag. */
1504
+ const REGISTRY_LATEST_URL = `https://registry.npmjs.org/dsh-lan-guard/latest`;
1505
+ /** Network timeout for the lookup. */
1506
+ const FETCH_TIMEOUT_MS = 5e3;
1507
+ /**
1508
+ * Parse a version string.
1509
+ *
1510
+ * @param value - e.g. `0.1.1`, `0.1.2-rc.1`.
1511
+ * @returns the parsed version, or `undefined` when it is not a version.
1512
+ */
1513
+ function parseVersion(value) {
1514
+ const match = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/.exec(value.trim());
1515
+ if (match === null) return void 0;
1516
+ return {
1517
+ major: Number(match[1]),
1518
+ minor: Number(match[2]),
1519
+ patch: Number(match[3]),
1520
+ prerelease: match[4] === void 0 ? [] : match[4].split(".")
1521
+ };
1522
+ }
1523
+ /** Compare two prerelease identifier lists per semver's precedence rules. */
1524
+ function comparePrerelease(left, right) {
1525
+ if (left.length === 0 && right.length === 0) return 0;
1526
+ if (left.length === 0) return 1;
1527
+ if (right.length === 0) return -1;
1528
+ const length = Math.max(left.length, right.length);
1529
+ for (let index = 0; index < length; index += 1) {
1530
+ const a = left[index];
1531
+ const b = right[index];
1532
+ if (a === void 0) return -1;
1533
+ if (b === void 0) return 1;
1534
+ const aNumber = /^\d+$/.test(a) ? Number(a) : void 0;
1535
+ const bNumber = /^\d+$/.test(b) ? Number(b) : void 0;
1536
+ if (aNumber !== void 0 && bNumber !== void 0) {
1537
+ if (aNumber !== bNumber) return aNumber < bNumber ? -1 : 1;
1538
+ continue;
1539
+ }
1540
+ if (aNumber !== void 0) return -1;
1541
+ if (bNumber !== void 0) return 1;
1542
+ if (a !== b) return a < b ? -1 : 1;
1543
+ }
1544
+ return 0;
1545
+ }
1546
+ /**
1547
+ * Compare two versions.
1548
+ *
1549
+ * @param left - first version.
1550
+ * @param right - second version.
1551
+ * @returns `-1`, `0` or `1`; an unparsable version compares as equal.
1552
+ */
1553
+ function compareVersions(left, right) {
1554
+ const a = parseVersion(left);
1555
+ const b = parseVersion(right);
1556
+ if (a === void 0 || b === void 0) return 0;
1557
+ if (a.major !== b.major) return a.major < b.major ? -1 : 1;
1558
+ if (a.minor !== b.minor) return a.minor < b.minor ? -1 : 1;
1559
+ if (a.patch !== b.patch) return a.patch < b.patch ? -1 : 1;
1560
+ return comparePrerelease(a.prerelease, b.prerelease);
1561
+ }
1562
+ /** Whether `latest` is newer than `current`. */
1563
+ function isNewer(latest, current) {
1564
+ return compareVersions(latest, current) > 0;
1565
+ }
1566
+ /** Reads the npm registry with a cache; never throws. */
1567
+ var UpdateChecker = class {
1568
+ #currentVersion;
1569
+ #logger;
1570
+ #fetch;
1571
+ #now;
1572
+ #ttlMs;
1573
+ #cached;
1574
+ constructor(options) {
1575
+ this.#currentVersion = options.currentVersion;
1576
+ this.#logger = options.logger ?? noopLogger;
1577
+ this.#fetch = options.fetchImpl ?? fetch;
1578
+ this.#now = options.now ?? (() => Date.now());
1579
+ this.#ttlMs = options.ttlMs ?? 216e5;
1580
+ }
1581
+ /** The version this process is running. */
1582
+ get currentVersion() {
1583
+ return this.#currentVersion;
1584
+ }
1585
+ /**
1586
+ * Look up the latest published version.
1587
+ *
1588
+ * @param options - `force` bypasses the cache.
1589
+ * @returns the status; a failure is reported in `error`, never thrown.
1590
+ */
1591
+ async check(options = {}) {
1592
+ if (options.force !== true && this.#cached !== void 0) {
1593
+ if (this.#now() - (this.#cached.checkedAtMs ?? 0) < this.#ttlMs) return this.#cached;
1594
+ }
1595
+ let status;
1596
+ try {
1597
+ const response = await this.#fetch(REGISTRY_LATEST_URL, {
1598
+ headers: { accept: "application/json" },
1599
+ signal: AbortSignal.timeout(FETCH_TIMEOUT_MS)
1600
+ });
1601
+ if (!response.ok) throw new Error(`registry ${String(response.status)}`);
1602
+ const body = await response.json();
1603
+ const latest = typeof body.version === "string" ? body.version : null;
1604
+ if (latest === null) throw new Error("registry response had no version");
1605
+ status = {
1606
+ current: this.#currentVersion,
1607
+ latest,
1608
+ hasUpdate: isNewer(latest, this.#currentVersion),
1609
+ checkedAtMs: this.#now(),
1610
+ error: null
1611
+ };
1612
+ if (status.hasUpdate) this.#logger.info("update available current=%s latest=%s", status.current, latest);
1613
+ } catch (error) {
1614
+ this.#logger.warn("update check failed name=%s", error.name);
1615
+ status = {
1616
+ current: this.#currentVersion,
1617
+ latest: null,
1618
+ hasUpdate: false,
1619
+ checkedAtMs: this.#now(),
1620
+ error: "registry_unavailable"
1621
+ };
1622
+ }
1623
+ this.#cached = status;
1624
+ return status;
1625
+ }
1626
+ };
1503
1627
  //#endregion
1504
1628
  //#region src/network.ts
1505
1629
  /**
@@ -2596,6 +2720,23 @@ function registerManagementRoutes(options) {
2596
2720
  path: `${MANAGEMENT_BASE}/devices`,
2597
2721
  handler: handleDevices
2598
2722
  }));
2723
+ /**
2724
+ * Update detection (SPEC F8). Read-only: it reports what npm has and the
2725
+ * settings page offers a copyable command — the host never installs anything.
2726
+ */
2727
+ disposers.push(options.webServer.register({
2728
+ kind: "exact",
2729
+ path: `${MANAGEMENT_BASE}/update`,
2730
+ handler: async (req, res) => {
2731
+ if (refusedByFence(req, res)) return;
2732
+ const url = new URL(req.url ?? "/", "http://dsh-lan-guard.invalid");
2733
+ const status = await options.updates.check({ force: url.searchParams.get("force") === "1" });
2734
+ sendJson(res, 200, {
2735
+ ok: true,
2736
+ ...status
2737
+ });
2738
+ }
2739
+ }));
2599
2740
  disposers.push(options.webServer.register({
2600
2741
  kind: "exact",
2601
2742
  path: `${MANAGEMENT_BASE}/port-check`,
@@ -3170,6 +3311,16 @@ const inject = ["webServer", "connection"];
3170
3311
  * @param rawConfig - the Loader config for this entry.
3171
3312
  * @returns the running runtime, or `undefined` when the plugin is switched off.
3172
3313
  */
3314
+ /** Read the plugin's own version (used by the update check). */
3315
+ async function readPackageVersion() {
3316
+ try {
3317
+ const raw = await readFile(new URL("../package.json", import.meta.url), "utf8");
3318
+ const parsed = JSON.parse(raw);
3319
+ return typeof parsed.version === "string" ? parsed.version : "0.0.0";
3320
+ } catch {
3321
+ return "0.0.0";
3322
+ }
3323
+ }
3173
3324
  async function startLanGuard(host, rawConfig) {
3174
3325
  const logger = host.logger ?? noopLogger;
3175
3326
  const config = parseConfig(rawConfig);
@@ -3194,6 +3345,10 @@ async function startLanGuard(host, rawConfig) {
3194
3345
  logger
3195
3346
  });
3196
3347
  await devices.init();
3348
+ const updates = new UpdateChecker({
3349
+ currentVersion: await readPackageVersion(),
3350
+ logger
3351
+ });
3197
3352
  const gate = new VisitorGate({
3198
3353
  auth,
3199
3354
  devices,
@@ -3246,7 +3401,8 @@ async function startLanGuard(host, rawConfig) {
3246
3401
  port: () => proxy.port,
3247
3402
  portFallback: () => proxy.portFallback
3248
3403
  },
3249
- devices
3404
+ devices,
3405
+ updates
3250
3406
  });
3251
3407
  if (proxy.portFallback) logger.warn("configured port %d was taken; listening on %d instead", proxy.requestedPort, proxy.port);
3252
3408
  logger.info("listening host=%s port=%d upstream=%s tls=%s gate=%s mode=%s", proxy.host, proxy.port, config.upstreamOrigin, config.tls.mode, switches.enabled() ? "on" : "off", switches.mode());
@@ -3254,6 +3410,7 @@ async function startLanGuard(host, rawConfig) {
3254
3410
  proxy,
3255
3411
  auth,
3256
3412
  devices,
3413
+ updates,
3257
3414
  async close() {
3258
3415
  mdns?.stop();
3259
3416
  disposeManagement?.();
@@ -3333,6 +3490,7 @@ async function apply(ctx, rawConfig) {
3333
3490
  access: deps.access,
3334
3491
  listener: deps.listener,
3335
3492
  devices: deps.devices,
3493
+ updates: deps.updates,
3336
3494
  logger: deps.logger
3337
3495
  })
3338
3496
  }, rawConfig);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-lan-guard",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "DSH web plugin: expose the official desktop Web UI to the local network through a gated reverse proxy, without touching DSH's own loopback binding.",
5
5
  "license": "MIT",
6
6
  "type": "module",