@ionic/discover
Advanced tools
Comparing version 0.0.4 to 0.0.5
export interface Interface { | ||
address: string; | ||
netmask: string; | ||
family: string; | ||
mac: string; | ||
internal: boolean; | ||
broadcast: string; | ||
} | ||
export declare class Publisher { | ||
private name; | ||
private port; | ||
private interval; | ||
private running; | ||
private timer; | ||
private client; | ||
private id; | ||
constructor(name: string, port: number, interval?: number); | ||
start(): void; | ||
namespace: string; | ||
name: string; | ||
port: number; | ||
interval: number; | ||
running: boolean; | ||
timer: any; | ||
client: any; | ||
id: string; | ||
constructor(namespace: string, name: string, port: number, interval?: number); | ||
start(callback?: Function): void; | ||
stop(): void; | ||
private getMessage(iface); | ||
buildMessage(address: string): string; | ||
private sayHello(); | ||
private getInterfaces(); | ||
} | ||
export declare function prepareInterfaces(interfaces: any): Interface[]; |
@@ -9,4 +9,5 @@ "use strict"; | ||
var Publisher = (function () { | ||
function Publisher(name, port, interval) { | ||
function Publisher(namespace, name, port, interval) { | ||
if (interval === void 0) { interval = 2000; } | ||
this.namespace = namespace; | ||
this.name = name; | ||
@@ -16,8 +17,10 @@ this.port = port; | ||
this.running = false; | ||
this.timer = null; | ||
this.client = null; | ||
if (name.indexOf(':') >= 0) { | ||
throw new Error('name can not contain ":"'); | ||
} | ||
this.id = Math.round(Math.random() * 1000000); | ||
this.id = Math.round(Math.random() * 1000000) + ''; | ||
} | ||
Publisher.prototype.start = function () { | ||
Publisher.prototype.start = function (callback) { | ||
var _this = this; | ||
@@ -32,2 +35,4 @@ if (this.running) { | ||
_this.timer = setInterval(_this.sayHello.bind(_this), _this.interval); | ||
_this.sayHello(); | ||
callback && callback(); | ||
}); | ||
@@ -46,10 +51,11 @@ client.bind(); | ||
}; | ||
Publisher.prototype.getMessage = function (iface) { | ||
Publisher.prototype.buildMessage = function (address) { | ||
var now = Date.now(); | ||
var message = { | ||
t: now, | ||
id: this.id + '', | ||
id: this.id, | ||
nspace: this.namespace, | ||
name: this.name, | ||
host: os.hostname(), | ||
ip: iface.address, | ||
ip: address, | ||
port: this.port | ||
@@ -62,3 +68,3 @@ }; | ||
var iface = _a[_i]; | ||
var message = new Buffer(this.getMessage(iface)); | ||
var message = new Buffer(this.buildMessage(iface.address)); | ||
this.client.send(message, 0, message.length, PORT, iface.broadcast); | ||
@@ -68,12 +74,3 @@ } | ||
Publisher.prototype.getInterfaces = function () { | ||
var ips = []; | ||
var interfaces = os.networkInterfaces(); | ||
return Object.keys(interfaces) | ||
.map(function (key) { return interfaces[key].find(function (i) { return i.family === 'IPv4'; }); }) | ||
.filter(function (iface) { return !!iface; }) | ||
.map(function (iface) { | ||
var i = Object.assign({}, iface); | ||
i.broadcast = computeMulticast(iface); | ||
return i; | ||
}); | ||
return prepareInterfaces(os.networkInterfaces()); | ||
}; | ||
@@ -83,6 +80,27 @@ return Publisher; | ||
exports.Publisher = Publisher; | ||
function computeMulticast(iface) { | ||
var ip = iface.address + '/' + iface.netmask; | ||
function prepareInterfaces(interfaces) { | ||
var set = new Set(); | ||
return Object.keys(interfaces) | ||
.map(function (key) { return interfaces[key]; }) | ||
.reduce(function (prev, current) { return prev.concat(current); }) | ||
.filter(function (iface) { return iface.family === 'IPv4'; }) | ||
.map(function (iface) { | ||
return { | ||
address: iface.address, | ||
broadcast: computeMulticast(iface.address, iface.netmask), | ||
}; | ||
}) | ||
.filter(function (iface) { | ||
if (!set.has(iface.broadcast)) { | ||
set.add(iface.broadcast); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
exports.prepareInterfaces = prepareInterfaces; | ||
function computeMulticast(address, netmask) { | ||
var ip = address + '/' + netmask; | ||
var block = new Netmask(ip); | ||
return block.broadcast; | ||
} |
{ | ||
"name": "@ionic/discover", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Simple UDP based protocol for service discovery implemented in pure JS. Not mDNS or bonjour. ", | ||
@@ -20,2 +20,3 @@ "main": "dist/index.js", | ||
"github-release": "node ./scripts/create-github-release.js", | ||
"test": "jest", | ||
"nightly": "npm run build && node ./scripts/publish-nightly.js" | ||
@@ -27,2 +28,3 @@ }, | ||
"devDependencies": { | ||
"@types/jest": "^20.0.8", | ||
"@types/node": "^8.0.18", | ||
@@ -32,3 +34,5 @@ "conventional-changelog-cli": "^1.3.2", | ||
"ionic-cz-conventional-changelog": "^1.0.0", | ||
"jest": "^20.0.4", | ||
"rimraf": "^2.6.1", | ||
"tslint": "^5.6.0", | ||
"tslint-ionic-rules": "0.0.11", | ||
@@ -49,3 +53,15 @@ "typescript": "^2.4.2" | ||
}, | ||
"typings": "dist/index.d.ts" | ||
"typings": "dist/index.d.ts", | ||
"jest": { | ||
"testEnvironment": "node", | ||
"transform": { | ||
"^.+\\.(ts)$": "<rootDir>/preprocessor.js" | ||
}, | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"js" | ||
], | ||
"testRegex": "/src/.*\\.spec\\.(ts|js)$", | ||
"coverageDirectory": "coverage" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11363
9
220
10