This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: All places in the API that used callbacks are now replaced with async/await
- Loading branch information
Showing
11 changed files
with
268 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,33 @@ | ||
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories. | ||
sudo: false | ||
language: node_js | ||
cache: npm | ||
|
||
matrix: | ||
include: | ||
- node_js: 6 | ||
env: CXX=g++-4.8 | ||
- node_js: 8 | ||
env: CXX=g++-4.8 | ||
# - node_js: stable | ||
# env: CXX=g++-4.8 | ||
stages: | ||
- check | ||
- test | ||
- cov | ||
|
||
node_js: | ||
- '10' | ||
|
||
script: | ||
- npm run lint | ||
- npm run test | ||
- npm run coverage | ||
os: | ||
- linux | ||
- osx | ||
|
||
before_script: | ||
- export DISPLAY=:99.0 | ||
- sh -e /etc/init.d/xvfb start | ||
script: npx nyc -s npm run test:node -- --bail | ||
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov | ||
|
||
jobs: | ||
include: | ||
- os: windows | ||
filter_secrets: false | ||
cache: false | ||
|
||
after_success: | ||
- npm run coverage-publish | ||
- stage: check | ||
script: | ||
- npx aegir build --bundlesize | ||
- npx aegir commitlint --travis | ||
- npx aegir dep-check | ||
- npm run lint | ||
|
||
addons: | ||
firefox: 'latest' | ||
apt: | ||
sources: | ||
- ubuntu-toolchain-r-test | ||
packages: | ||
- g++-4.8 | ||
notifications: | ||
email: false |
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 withIs = require('class-is') | ||
const toPull = require('async-iterator-to-pull-stream') | ||
const error = require('pull-stream/sources/error') | ||
const drain = require('pull-stream/sinks/drain') | ||
const TCP = require('./') | ||
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 TcpAdapter extends TCP { | ||
dial (ma, options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
callback = callback || noop | ||
|
||
const conn = new Connection() | ||
|
||
super.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 = super.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 = withIs(TcpAdapter, { | ||
className: 'TCP', | ||
symbolName: '@libp2p/js-libp2p-tcp/tcp' | ||
}) |
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,8 @@ | ||
'use strict' | ||
|
||
// IPFS multi-address code | ||
module.exports.IPFS_MA_CODE = 421 | ||
|
||
// Time to wait for a connection to close gracefully before destroying it | ||
// manually | ||
module.exports.CLOSE_TIMEOUT = 2000 |
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
Oops, something went wrong.