This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: callbacks -> async / await (#44)
* feat: callbacks -> async / await BREAKING CHANGE: All places in the API that used callbacks are now replaced with async/await * test: add tests for canceling dials * feat: Adapter class
- Loading branch information
Showing
7 changed files
with
293 additions
and
158 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,11 +38,15 @@ | |
"dirty-chai": "^2.0.1" | ||
}, | ||
"dependencies": { | ||
"abort-controller": "^3.0.0", | ||
"async-iterator-to-pull-stream": "^1.3.0", | ||
"chai": "^4.2.0", | ||
"interface-connection": "~0.3.3", | ||
"it-goodbye": "^2.0.0", | ||
"it-pipe": "^1.0.0", | ||
"multiaddr": "^5.0.2", | ||
"pull-goodbye": "~0.0.2", | ||
"pull-serializer": "~0.3.2", | ||
"pull-stream": "^3.6.9" | ||
"pull-stream": "^3.6.9", | ||
"streaming-iterables": "^4.0.2" | ||
}, | ||
"contributors": [ | ||
"David Dias <[email protected]>", | ||
|
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,80 @@ | ||
'use strict' | ||
|
||
const { Connection } = require('interface-connection') | ||
const toPull = require('async-iterator-to-pull-stream') | ||
const error = require('pull-stream/sources/error') | ||
const drain = require('pull-stream/sinks/drain') | ||
const noop = () => {} | ||
|
||
function callbackify (fn) { | ||
return async function (...args) { | ||
let cb = args.pop() | ||
if (typeof cb !== 'function') { | ||
args.push(cb) | ||
cb = noop | ||
} | ||
let res | ||
try { | ||
res = await fn(...args) | ||
} catch (err) { | ||
return cb(err) | ||
} | ||
cb(null, res) | ||
} | ||
} | ||
|
||
// Legacy adapter to old transport & connection interface | ||
class Adapter { | ||
constructor (transport) { | ||
this.transport = transport | ||
} | ||
|
||
dial (ma, options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
callback = callback || noop | ||
|
||
const conn = new Connection() | ||
|
||
this.transport.dial(ma, options) | ||
.then(socket => { | ||
conn.setInnerConn(toPull.duplex(socket)) | ||
conn.getObservedAddrs = callbackify(socket.getObservedAddrs.bind(socket)) | ||
conn.close = callbackify(socket.close.bind(socket)) | ||
callback(null, conn) | ||
}) | ||
.catch(err => { | ||
conn.setInnerConn({ sink: drain(), source: error(err) }) | ||
callback(err) | ||
}) | ||
|
||
return conn | ||
} | ||
|
||
createListener (options, handler) { | ||
if (typeof options === 'function') { | ||
handler = options | ||
options = {} | ||
} | ||
|
||
const server = this.transport.createListener(options, socket => { | ||
const conn = new Connection(toPull.duplex(socket)) | ||
conn.getObservedAddrs = callbackify(socket.getObservedAddrs.bind(socket)) | ||
handler(conn) | ||
}) | ||
|
||
const proxy = { | ||
listen: callbackify(server.listen.bind(server)), | ||
close: callbackify(server.close.bind(server)), | ||
getAddrs: callbackify(server.getAddrs.bind(server)), | ||
getObservedAddrs: callbackify(() => server.getObservedAddrs()) | ||
} | ||
|
||
return new Proxy(server, { get: (_, prop) => proxy[prop] || server[prop] }) | ||
} | ||
} | ||
|
||
module.exports = Adapter |
Oops, something went wrong.