From 4f3ce393f4b5bf1c4d03e8fd1f6931e90fb24396 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 3 Dec 2018 13:03:24 +0000 Subject: [PATCH] Actually parse out TXT data, flawed though it must be --- packages/driver-mdns/src/index.js | 30 ++++++++++++++----- .../syntax-playground/src/avahipublish.js | 6 ++-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/driver-mdns/src/index.js b/packages/driver-mdns/src/index.js index 8c95a0a..92f8cfd 100644 --- a/packages/driver-mdns/src/index.js +++ b/packages/driver-mdns/src/index.js @@ -126,14 +126,29 @@ spawn named 'driver/avahi-browse' { react { on retracted S.Readable(stdout) topFacet.stop(); on message S.Line(stdout, $line) { - // We supply a `9` argument to `split` in order to ignore - // fields past the port number, because parsing of TXT - // record data is unreliable given the way avahi-browse - // formats it. + // Parsing of TXT record data (appearing after the port + // number in an '=' record) is unreliable given the way + // avahi-browse formats it. // // See https://github.com/lathiat/avahi/pull/206. // - send BrowserInput(id, line.toString('utf-8').split(/;/, 9)); + // However, it's still useful to have, so we do our best! + // + const pieces = line.toString('utf-8').split(/;/); + if (pieces[0] === '=') { + // A resolved address record, which has TXT data. + const normalFields = pieces.slice(0, 9); + const txtFields = pieces.slice(9).join(';'); // it's these that are dodgy + if (txtFields === '') { + normalFields.push([]); + } else { + normalFields.push(txtFields.slice(1,-1).split(/" "/)); // OMG this is vile + } + send BrowserInput(id, normalFields); + } else { + // Something else. + send BrowserInput(id, pieces); + } } on message BrowserInput(id, ["+", $interfaceName, $family, $name, $serviceType, $domain]) { @@ -143,11 +158,12 @@ spawn named 'driver/avahi-browse' { id, ["-", interfaceName, family, name, serviceType, domain]); on message BrowserInput( id, ['=', interfaceName, family, name, serviceType, domain, - $hostName, $address, $portStr]) + $hostName, $address, $portStr, $txtDataRecords]) { const port0 = Number(portStr); const port = Number.isNaN(port0) ? null : port0; - react assert Discovered(svc, hostName, port, [], address, family, interfaceName); + react assert Discovered( + svc, hostName, port, txtDataRecords, address, family, interfaceName); } } } diff --git a/packages/syntax-playground/src/avahipublish.js b/packages/syntax-playground/src/avahipublish.js index 5de617b..667e298 100644 --- a/packages/syntax-playground/src/avahipublish.js +++ b/packages/syntax-playground/src/avahipublish.js @@ -18,10 +18,10 @@ spawn named 'test' { on stop console.log('-', name, hostName, port, txtDataRecords, address, interfaceName); } - during M.Discovered(M.Service($n, $t), $h, $p, _, $a, "IPv4", $i) { + during M.Discovered(M.Service($n, $t), $h, $p, $d, $a, "IPv4", $i) { if (t !== '_syndicate._tcp') { - on start console.log('**', t, n, h, p, a, i); - on stop console.log('==', t, n, h, p, a, i); + on start console.log('**', t, n, h, p, d, a, i); + on stop console.log('==', t, n, h, p, d, a, i); } } }