Skip to content

fix: the API of es6-promisify is not the same as promisify-es6 #905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
},
"devDependencies": {
"@nodeutils/defaults-deep": "^1.1.0",
"@types/es6-promisify": "^6.0.0",
"abortable-iterator": "^3.0.0",
"aegir": "^29.2.0",
"chai-bytes": "^0.1.2",
Expand Down
28 changes: 23 additions & 5 deletions src/nat-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const NatAPI = require('@motrix/nat-api')
const debug = require('debug')
const promisify = require('es6-promisify')
const { promisify } = require('es6-promisify')
const Multiaddr = require('multiaddr')
const log = Object.assign(debug('libp2p:nat'), {
error: debug('libp2p:nat:err')
Expand Down Expand Up @@ -132,14 +132,32 @@ class NatManager {
}

const client = new NatAPI(this._options)
const map = promisify(client.map, { context: client })
const destroy = promisify(client.destroy, { context: client })
const externalIp = promisify(client.externalIp, { context: client })

/** @type {(...any) => any} */
const map = promisify(client.map.bind(client))
/** @type {(...any) => any} */
const destroy = promisify(client.destroy.bind(client))
/** @type {(...any) => any} */
const externalIp = promisify(client.externalIp.bind(client))

// these are all network operations so add a retry
this._client = {
// these are all network operations so add a retry
/**
* @param {...any} args
* @returns {Promise<void>}
*/
map: (...args) => retry(() => map(...args), { onFailedAttempt: log.error, unref: true }),

/**
* @param {...any} args
* @returns {Promise<void>}
*/
destroy: (...args) => retry(() => destroy(...args), { onFailedAttempt: log.error, unref: true }),

/**
* @param {...any} args
* @returns {Promise<string>}
*/
externalIp: (...args) => retry(() => externalIp(...args), { onFailedAttempt: log.error, unref: true })
}

Expand Down
8 changes: 4 additions & 4 deletions test/nat-manager/nat-manager.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('Nat Manager (TCP)', () => {
natManager,
addressManager
} = await createNatManager([
'/ip6/::/tcp/5001'
'/ip6/::/tcp/0'
])

let observed = addressManager.getObservedAddrs().map(ma => ma.toString())
Expand All @@ -173,7 +173,7 @@ describe('Nat Manager (TCP)', () => {
natManager,
addressManager
} = await createNatManager([
'/ip6/::1/tcp/5001'
'/ip6/::1/tcp/0'
])

let observed = addressManager.getObservedAddrs().map(ma => ma.toString())
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('Nat Manager (TCP)', () => {
natManager,
addressManager
} = await createNatManager([
'/ip4/127.0.0.1/tcp/5900'
'/ip4/127.0.0.1/tcp/0'
])

let observed = addressManager.getObservedAddrs().map(ma => ma.toString())
Expand All @@ -224,7 +224,7 @@ describe('Nat Manager (TCP)', () => {
natManager,
addressManager
} = await createNatManager([
'/ip4/0.0.0.0/tcp/5900/sctp/49832'
'/ip4/0.0.0.0/tcp/0/sctp/0'
])

let observed = addressManager.getObservedAddrs().map(ma => ma.toString())
Expand Down