From 3958a2fab92d274b7fd04674e0a1e81e2d779c30 Mon Sep 17 00:00:00 2001 From: Julian Waller Date: Fri, 9 Aug 2024 23:22:46 +0100 Subject: [PATCH] feat: use unique installation name in mdns --- satellite/package.json | 1 + satellite/src/config.ts | 10 +++++++++- satellite/src/mdnsAnnouncer.ts | 4 ++-- yarn.lock | 10 ++++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/satellite/package.json b/satellite/package.json index f6c5809..3bc4792 100644 --- a/satellite/package.json +++ b/satellite/package.json @@ -64,6 +64,7 @@ "koa-body": "^6.0.1", "koa-router": "^12.0.1", "koa-static": "^5.0.0", + "nanoid": "^5.0.7", "node-hid": "^3.1.0", "semver": "^7.6.3", "tslib": "^2.6.3", diff --git a/satellite/src/config.ts b/satellite/src/config.ts index cf5fd5d..6972b3b 100644 --- a/satellite/src/config.ts +++ b/satellite/src/config.ts @@ -1,5 +1,9 @@ import Conf, { Schema } from 'conf' import path from 'path' +import os from 'os' +import { nanoid, customAlphabet } from 'nanoid' + +const nanoidHex = customAlphabet('0123456789abcdef') export interface SatelliteConfig { remoteIp: string @@ -30,7 +34,7 @@ export const satelliteConfigSchema: Schema = { installationName: { type: 'string', description: 'Name for this Satellite installation', - default: 'TODO - something here', + default: `Satellite ${os.hostname()} (${nanoidHex(8)})`, }, restEnabled: { @@ -53,12 +57,16 @@ export const satelliteConfigSchema: Schema = { } export function ensureFieldsPopulated(store: Conf): void { + // Note: This doesn't appear to do anything, as Conf is populated with defaults for (const [key, schema] of Object.entries(satelliteConfigSchema)) { if (store.get(key) === undefined && schema.default !== undefined) { // Ensure values are written to disk store.set(key, schema.default) } } + + // Ensure that the store with the filled in defaults is written to disk + store.store = store.store } export function openHeadlessConfig(rawConfigPath: string): Conf { diff --git a/satellite/src/mdnsAnnouncer.ts b/satellite/src/mdnsAnnouncer.ts index d8ef17f..cb3e937 100644 --- a/satellite/src/mdnsAnnouncer.ts +++ b/satellite/src/mdnsAnnouncer.ts @@ -38,11 +38,11 @@ export class MdnsAnnouncer { try { const restEnabled = this.#appConfig.get('restEnabled') const restPort = this.#appConfig.get('restPort') + const installationName = this.#appConfig.get('installationName') || os.hostname() || 'Unnamed Satellite' this.#bonjourService = this.#bonjour.publish( { - // TODO - this name needs to be unique for each installation - name: os.hostname(), // TODO - something customisable? + name: installationName, type: 'companion-satellite', protocol: 'tcp', port: restPort || 9999, diff --git a/yarn.lock b/yarn.lock index 22611d4..a8beec4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5385,6 +5385,15 @@ __metadata: languageName: node linkType: hard +"nanoid@npm:^5.0.7": + version: 5.0.7 + resolution: "nanoid@npm:5.0.7" + bin: + nanoid: bin/nanoid.js + checksum: 10c0/a3fb1c157e3e35378f44e5a7130c70f80c9037f66c9a37285e5e3d8298e8405fcb2399baaa420980b0fe5fd9c2e4186a6a31c3526f21de03cf34c1b459871401 + languageName: node + linkType: hard + "natural-compare@npm:^1.4.0": version: 1.4.0 resolution: "natural-compare@npm:1.4.0" @@ -6443,6 +6452,7 @@ __metadata: koa-body: "npm:^6.0.1" koa-router: "npm:^12.0.1" koa-static: "npm:^5.0.0" + nanoid: "npm:^5.0.7" node-hid: "npm:^3.1.0" rimraf: "npm:^5.0.9" semver: "npm:^7.6.3"