Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Add typings #84

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .jsdoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"recurseDepth": 10,
"source": {
"include": ["src/"],
"exclude": ["node_modules"]
},
"opts": {
"destination": "src/",
"template": "./node_modules/tsd-jsdoc/dist",
"recurse": true
}
}
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
- npx aegir commitlint --travis
- npx aegir dep-check
- npm run lint
- npm run generate-typings

notifications:
email: false
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"release": "aegir release -t node --no-build",
"release-minor": "aegir release --type minor -t node --no-build",
"release-major": "aegir release --type major -t node --no-build",
"coverage-publish": "aegir coverage publish"
"coverage-publish": "aegir coverage publish",
"generate-typings": "./node_modules/.bin/jsdoc -c ./.jsdoc.json"
},
"pre-push": [
"lint"
Expand All @@ -36,7 +37,10 @@
"devDependencies": {
"aegir": "^18.2.2",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1"
"dirty-chai": "^2.0.1",
"jsdoc": "^3.6.3",
"tsd-jsdoc": "^2.3.1",
"typescript-definition-tester": "0.0.6"
},
"dependencies": {
"async": "^2.6.2",
Expand Down
24 changes: 21 additions & 3 deletions src/compat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@ const parallel = require('async/parallel')
const Responder = require('./responder')
const Querier = require('./querier')

/**
* @class
*/
class GoMulticastDNS extends EE {
/**
*
* @param {object} peerInfo
*/
constructor (peerInfo) {
super()
this._started = false
this._peerInfo = peerInfo
this._onPeer = this._onPeer.bind(this)
}

/**
*
* @param {function} callback
* @returns {functio}
*/
start (callback) {
if (this._started) {
return callback(new Error('MulticastDNS service is already started'))
Expand All @@ -31,11 +42,18 @@ class GoMulticastDNS extends EE {
cb => this._querier.start(cb)
], callback)
}

/**
*
* @param {object} peerInfo
*/
_onPeer (peerInfo) {
this.emit('peer', peerInfo)
}

/**
*
* @param {function} callback
* @returns {?object}
*/
stop (callback) {
if (!this._started) {
return callback(new Error('MulticastDNS service is not started'))
Expand Down
23 changes: 22 additions & 1 deletion src/compat/querier.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ const nextTick = require('async/nextTick')
const log = require('debug')('libp2p:mdns:compat:querier')
const { SERVICE_TAG_LOCAL, MULTICAST_IP, MULTICAST_PORT } = require('./constants')

/**
* @class
*/
class Querier extends EE {
/**
*
* @param {object} peerId
* @param {object} options
*/
constructor (peerId, options) {
super()
assert(peerId, 'missing peerId parameter')
Expand All @@ -28,6 +36,10 @@ class Querier extends EE {
this._onResponse = this._onResponse.bind(this)
}

/**
*
* @param {function} callback
*/
start (callback) {
this._handle = periodically(() => {
// Create a querier that queries multicast but gets responses unicast
Expand Down Expand Up @@ -57,6 +69,12 @@ class Querier extends EE {
nextTick(() => callback())
}

/**
*
* @param {object} event
* @param {object} info
* @returns {?object}
*/
_onResponse (event, info) {
const answers = event.answers || []
const ptrRecord = answers.find(a => a.type === 'PTR' && a.name === SERVICE_TAG_LOCAL)
Expand Down Expand Up @@ -115,7 +133,10 @@ class Querier extends EE {
this.emit('peer', info)
})
}

/**
*
* @param {function} callback
*/
stop (callback) {
this._handle.stop(callback)
}
Expand Down
7 changes: 7 additions & 0 deletions src/compat/responder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ const { SERVICE_TAG_LOCAL } = require('./constants')

const tcp = new TCP()

/**
* @class
*/
class Responder {
/**
*
* @param {object} peerInfo
*/
constructor (peerInfo) {
assert(peerInfo, 'missing peerInfo parameter')
this._peerInfo = peerInfo
Expand Down
23 changes: 20 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ const log = debug('libp2p:mdns')
const query = require('./query')
const GoMulticastDNS = require('./compat')

/**
* @class
*/
class MulticastDNS extends EventEmitter {
/**
*
* @param {object} options
*/
constructor (options) {
super()
assert(options.peerInfo, 'needs a PeerInfo to work')
Expand All @@ -31,7 +38,10 @@ class MulticastDNS extends EventEmitter {
this._goMdns.on('peer', this._onPeer)
}
}

/**
*
* @param {function} callback
*/
start (callback) {
const mdns = multicastDNS({ port: this.port })

Expand Down Expand Up @@ -59,11 +69,18 @@ class MulticastDNS extends EventEmitter {
nextTick(() => callback())
}
}

/**
*
* @param {object} peerInfo
*/
_onPeer (peerInfo) {
this.emit('peer', peerInfo)
}

/**
*
* @param {function} callback
* @returns {function}
*/
stop (callback) {
if (!this.mdns) {
return callback(new Error('MulticastDNS service had not started yet'))
Expand Down
95 changes: 95 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
*
* @param {object} peerInfo
*/
declare class GoMulticastDNS {
constructor(peerInfo: any);
/**
*
* @param {function} callback
*/
start(callback: (...params: any[]) => any): void;
/**
*
* @param {object} peerInfo
*/
_onPeer(peerInfo: any): void;
/**
*
* @param {function} callback
*/
stop(callback: (...params: any[]) => any): void;
}

/**
*
* @param {object} peerId
* @param {object} options
*/
declare class Querier {
constructor(peerId: any, options: any);
/**
*
* @param {function} callback
*/
start(callback: (...params: any[]) => any): void;
/**
*
* @param {object} event
* @param {object} info
*/
_onResponse(event: any, info: any): void;
/**
*
* @param {function} callback
*/
stop(callback: (...params: any[]) => any): void;
}

/**
* Run `fn` for a certain period of time, and then wait for an interval before
* running it again. `fn` must return an object with a stop function, which is
* called when the period expires.
*
* @param {Function} fn function to run
* @param {Object} [options]
* @param {Object} [options.period] Period in ms to run the function for
* @param {Object} [options.interval] Interval in ms between runs
* @returns {Object} handle that can be used to stop execution
*/
declare function periodically(fn: (...params: any[]) => any, options?: {
period?: any;
interval?: any;
}): any;

/**
*
* @param {object} peerInfo
*/
declare class Responder {
constructor(peerInfo: any);
}

/**
*
* @param {object} options
*/
declare class MulticastDNS {
constructor(options: any);
/**
*
* @param {function} callback
*/
start(callback: (...params: any[]) => any): void;
/**
*
* @param {object} peerInfo
*/
_onPeer(peerInfo: any): void;
/**
*
* @param {function} callback
*/
stop(callback: (...params: any[]) => any): void;
}