forked from watson/bonjour
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced dns-txt with first party handler and upgraded multicast-dns@…
…7.2.3 This commit resolves the issue with multicast-dns@6.2.3 #2
- Loading branch information
Showing
7 changed files
with
261 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict' | ||
|
||
type KeyValue = { [key: string]: any } | ||
|
||
export class DnsTxt { | ||
|
||
private binary: boolean | ||
|
||
constructor(opts: KeyValue = {}) { | ||
this.binary = opts ? opts.binary : false | ||
} | ||
|
||
/** | ||
* Encode the KeyValue to buffer | ||
* @param data | ||
* @returns | ||
*/ | ||
public encode(data: KeyValue = {}) { | ||
return Object.entries(data) | ||
.map(([key, value]) => { | ||
let item: string = `${key}=${value}` | ||
return Buffer.from(item) | ||
}) | ||
} | ||
|
||
/** | ||
* Decode the buffer to KeyValue | ||
* @param buffer | ||
* @returns | ||
*/ | ||
public decode(buffer: Buffer): KeyValue { | ||
var data: KeyValue = {} | ||
// Format buffer to KeyValue | ||
try { | ||
let format : string = buffer.toString() | ||
let parts : Array<any> = format.split(/=(.+)/) | ||
let key : string = parts[0] | ||
let value : any = parts[1] | ||
data[key] = value | ||
} catch(_) {} | ||
// Return data a KeyValue | ||
return data | ||
} | ||
|
||
/** | ||
* Decode all buffer items to KeyValye | ||
* @param buffer | ||
* @returns | ||
*/ | ||
public decodeAll(buffer: Array<Buffer>) { | ||
return buffer | ||
.filter(i => i.length > 1) //i.toString() != '' | ||
.map(i => this.decode(i)) | ||
.reduce((prev, curr) => { | ||
var obj = prev | ||
let [key] = Object.keys(curr) | ||
let [value] = Object.values(curr) | ||
obj[key] = value | ||
return obj | ||
}, {}) | ||
} | ||
|
||
} | ||
|
||
export default DnsTxt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.