@cubicecho/discovery-core 1.1.0 → 1.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.
@@ -91,4 +91,5 @@ await watch.stop();
91
91
  - An `onUp` or `onDown` that throws is reported through `onError`, and the other entries are still reported. A promise it returns isn't awaited, so catch inside an async handler.
92
92
  - `createEntryTracker` is the same logic without mDNS, driven by `hostUp` and `hostDown`, for tests or another source of hosts.
93
93
 
94
- A process that also advertises sees itself; skip its own instance name. Anything on the LAN can advertise, so decide what to connect to by policy, not by name.
94
+ A process that also advertises sees itself. Skip its own instance name, and also any host for which `isOwnService(host, ownPort)` is true: announced from this machine (its mDNS name or one of its interface addresses) on the port the process advertises. Other services on the same machine use other ports, so they are still found.
95
+ Anything on the LAN can advertise, so decide what to connect to by policy, not by name.
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Telling a host found on the network apart from this process's own announcement.
3
+ */
4
+ import type { FoundHost } from './browse.ts';
5
+ /** How this machine shows up on the network. */
6
+ export interface ThisMachine {
7
+ /** Its mDNS hostname, e.g. `nas.local`: what `advertiseService` announces when given no `host`. */
8
+ host: string;
9
+ /** Every address its network interfaces have, loopback included. */
10
+ addresses: string[];
11
+ }
12
+ /**
13
+ * This machine's mDNS hostname and the addresses of its network interfaces, read now.
14
+ *
15
+ * @returns The machine.
16
+ */
17
+ export declare function thisMachine(): ThisMachine;
18
+ /**
19
+ * Whether a found host is this process's own announcement: announced from this machine, on the port it advertises.
20
+ *
21
+ * @param found - A host from a browse or watch.
22
+ * @param ownPort - The port this process advertises.
23
+ * @param [machine] - This machine; read from the OS when unset.
24
+ * @returns True when the host is this machine (by its mDNS name or one of its addresses) and the port is `ownPort`.
25
+ *
26
+ * @remarks
27
+ * Use it beside a check of the instance name: it still holds when the name is not the one expected. Other services
28
+ * on this machine listen on other ports, so they are still found.
29
+ */
30
+ export declare function isOwnService(found: Pick<FoundHost<unknown>, 'host' | 'addresses' | 'port'>, ownPort: number, machine?: ThisMachine): boolean;
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Telling a host found on the network apart from this process's own announcement.
3
+ */
4
+ import { hostname, networkInterfaces } from 'node:os';
5
+ /** The domain every mDNS hostname lives under. */
6
+ const MDNS_DOMAIN = '.local';
7
+ /** The prefix an IPv4 address carries when written as IPv6. */
8
+ const IPV4_MAPPED_PREFIX = '::ffff:';
9
+ /**
10
+ * Writes an address the one way it is compared: lowercase, with no IPv6 zone and no IPv4-mapped prefix.
11
+ *
12
+ * @param address - An IPv4 or IPv6 address.
13
+ * @returns The address, normalized.
14
+ */
15
+ function normalizeAddress(address) {
16
+ const withoutZone = address.toLowerCase().split('%')[0] ?? '';
17
+ return withoutZone.startsWith(IPV4_MAPPED_PREFIX) ? withoutZone.slice(IPV4_MAPPED_PREFIX.length) : withoutZone;
18
+ }
19
+ /**
20
+ * Writes an mDNS hostname the one way it is compared: lowercase, with no trailing dot.
21
+ *
22
+ * @param name - A hostname.
23
+ * @returns The name, normalized.
24
+ */
25
+ function normalizeHost(name) {
26
+ return name.toLowerCase().replace(/\.$/, '');
27
+ }
28
+ /**
29
+ * This machine's mDNS hostname and the addresses of its network interfaces, read now.
30
+ *
31
+ * @returns The machine.
32
+ */
33
+ export function thisMachine() {
34
+ const addresses = Object.values(networkInterfaces()).flatMap((entries) => (entries ?? []).map((entry) => entry.address));
35
+ return { host: `${hostname()}${MDNS_DOMAIN}`, addresses };
36
+ }
37
+ /**
38
+ * Whether a found host is this process's own announcement: announced from this machine, on the port it advertises.
39
+ *
40
+ * @param found - A host from a browse or watch.
41
+ * @param ownPort - The port this process advertises.
42
+ * @param [machine] - This machine; read from the OS when unset.
43
+ * @returns True when the host is this machine (by its mDNS name or one of its addresses) and the port is `ownPort`.
44
+ *
45
+ * @remarks
46
+ * Use it beside a check of the instance name: it still holds when the name is not the one expected. Other services
47
+ * on this machine listen on other ports, so they are still found.
48
+ */
49
+ export function isOwnService(found, ownPort, machine = thisMachine()) {
50
+ if (found.port !== ownPort) {
51
+ return false;
52
+ }
53
+ if (normalizeHost(found.host) === normalizeHost(machine.host)) {
54
+ return true;
55
+ }
56
+ const own = new Set(machine.addresses.map(normalizeAddress));
57
+ return found.addresses.some((address) => own.has(normalizeAddress(address)));
58
+ }
package/dist/mdns.d.ts CHANGED
@@ -8,6 +8,7 @@ export { AdvertiseError, type Advertisement, advertiseService, type ServiceAnnou
8
8
  export { type Advertiser, type AdvertiserDeps, type AdvertiserOptions, createAdvertiser } from './mdns/advertiser.ts';
9
9
  export { type FoundHost, type FoundService, findServices, type ServiceBrowse, type ServiceWatch, type ServiceWatchHandlers, toFoundHost, watchServices, } from './mdns/browse.ts';
10
10
  export { createEntryTracker, type EntrySource, type EntryTracker, type EntryWatchHandlers, type HostRead, } from './mdns/entry-tracker.ts';
11
+ export { isOwnService, type ThisMachine, thisMachine } from './mdns/own-service.ts';
11
12
  export { acquireResponder, type ResponderLease, responderHolders } from './mdns/responder.ts';
12
13
  export { assertLocalPaths, FLAG_ON, isLocalPath, readTxtFlag, readTxtPath, TXT_TLS_KEY, TXT_VERSION_KEY, } from './mdns/txt.ts';
13
14
  export { type EntryWatch, watchEntries } from './mdns/watch-entries.ts';
package/dist/mdns.js CHANGED
@@ -8,6 +8,7 @@ export { AdvertiseError, advertiseService, } from './mdns/advertise-service.js';
8
8
  export { createAdvertiser } from './mdns/advertiser.js';
9
9
  export { findServices, toFoundHost, watchServices, } from './mdns/browse.js';
10
10
  export { createEntryTracker, } from './mdns/entry-tracker.js';
11
+ export { isOwnService, thisMachine } from './mdns/own-service.js';
11
12
  export { acquireResponder, responderHolders } from './mdns/responder.js';
12
13
  export { assertLocalPaths, FLAG_ON, isLocalPath, readTxtFlag, readTxtPath, TXT_TLS_KEY, TXT_VERSION_KEY, } from './mdns/txt.js';
13
14
  export { watchEntries } from './mdns/watch-entries.js';
package/llms.txt CHANGED
@@ -72,6 +72,9 @@ LAN advertising and browsing over mDNS / DNS-SD, for any service type, on one re
72
72
  - `EntryTracker` (type): Tracks the entries on a set of hosts.
73
73
  - `EntryWatchHandlers` (type): What an entry watch tells its caller.
74
74
  - `HostRead` (type): What one read of a host's document gave.
75
+ - `isOwnService`: Whether a found host is this process's own announcement: announced from this machine, on the port it advertises.
76
+ - `ThisMachine` (type): How this machine shows up on the network.
77
+ - `thisMachine`: This machine's mDNS hostname and the addresses of its network interfaces, read now.
75
78
  - `acquireResponder`: Opens the shared responder, or joins the one already open.
76
79
  - `ResponderLease` (type): A hold on the shared responder; release it when done.
77
80
  - `responderHolders`: How many leases the shared responder has open, for tests and diagnostics.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cubicecho/discovery-core",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "The shared core of the cubicecho discovery packages: serving and fetching public discovery documents, and mDNS advertising and browsing on one responder per process.",
5
5
  "keywords": [
6
6
  "discovery",