This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
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
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.