Skip to content

Commit

Permalink
fix(discovery-client): Monkeypatch uncatchable throw from mdns-js (#2433
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mcous authored Oct 5, 2018
1 parent b165b17 commit c177f87
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
4 changes: 3 additions & 1 deletion discovery-client/__mocks__/mdns-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ const __mockReset = () => {
createBrowser.mockReturnValue(__mockBrowser)
}

module.exports = {tcp, createBrowser, __mockBrowser, __mockReset}
const ServiceType = function () {}

module.exports = {tcp, createBrowser, ServiceType, __mockBrowser, __mockReset}
5 changes: 2 additions & 3 deletions discovery-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import EventEmitter from 'events'
import escape from 'escape-string-regexp'
import mdns from 'mdns-js'
import toRegex from 'to-regex'
import differenceBy from 'lodash/differenceBy'
import xorBy from 'lodash/xorBy'

import MdnsBrowser from './mdns-browser'
import {poll, stop, type PollRequest} from './poller'
import {
createServiceList,
Expand Down Expand Up @@ -178,8 +178,7 @@ export class DiscoveryClient extends EventEmitter {
_startBrowser (): void {
this._stopBrowser()

const browser = mdns
.createBrowser(mdns.tcp('http'))
const browser = MdnsBrowser()
.once('ready', () => browser.discover())
.on('update', service => this._handleUp(service))
.on('error', error => this.emit('error', error))
Expand Down
23 changes: 23 additions & 0 deletions discovery-client/src/mdns-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @flow
// mdns browser wrapper
import mdns, {ServiceType} from 'mdns-js'
import type {Browser} from 'mdns-js'

monkeyPatchThrowers()

export default function MdnsBrowser (): Browser {
return mdns.createBrowser(mdns.tcp('http'))
}

function monkeyPatchThrowers () {
// this method can throw (without emitting), so we need to patch this up
const originalServiceTypeFromString = ServiceType.prototype.fromString

ServiceType.prototype.fromString = function (...args) {
try {
originalServiceTypeFromString.apply(this, args)
} catch (error) {
console.warn(error)
}
}
}
9 changes: 5 additions & 4 deletions flow-typed/npm/mdns-js_v1.0.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ declare module 'mdns-js' {
}

declare module.exports: {
createBrowser: (serviceType: ServiceType) => Browser,
tcp: (type: string) => ServiceType
createBrowser: (serviceType: MdnsServiceType) => Browser,
tcp: (type: string) => MdnsServiceType,
ServiceType: any
}

declare type Browser = mdns$Browser

declare type ServiceType = mdns$ServiceType
declare type MdnsServiceType = mdns$ServiceType

declare type BrowserService = {
addresses: Array<string>,
query: Array<string>,
type?: Array<ServiceType>,
type?: Array<MdnsServiceType>,
txt?: Array<string>,
port?: number,
fullname?: string,
Expand Down

0 comments on commit c177f87

Please sign in to comment.