Skip to content

Commit

Permalink
fix: Wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 committed Aug 2, 2018
1 parent 7abb372 commit 48e2beb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "teletunnel-core",
"version": "0.0.1",
"description": "The core module of the teletunnel protocol",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"lint": "aegir lint",
"test": "aegir test",
Expand Down
11 changes: 8 additions & 3 deletions src/sorting-hat.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use strict'

const fwAddr = require('forward-addr')
const wrapper = require('./wrapper')

async function processConn (conn, connState, protocols, handlers, on404Handler) {
async function processConn (conn, {timeout, protocols, handlers, on404}, connState) {
if (!connState) connState = []

const wrapped = wrapper({conn, timeout: timeout || 2000})
let result = await Promise.all(protocols.map(async (proto) => {
try {
let res = await proto.detect(conn)
let res = await proto.detect(wrapped.createReader()) // TODO: evaluate if reader needs cleanup (abort src) or if gc does everything?
if (!res) return res
return [proto.name, res.props, res.state]
if (!res.props) return [proto.name, res]
return [proto.name, res.props, res.state] // this is for more complex protos that need to pass a state to .stream() or subprotos
} catch (e) {
return false
}
Expand Down
4 changes: 3 additions & 1 deletion src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ module.exports = ({timeout, conn}) => {
read: (bytes) => new Promise((resolve, reject) => {
reader.read(bytes, (err, res) => err ? reject(err) : resolve(res))
}),
getObservedAddrs: conn.getObservedAddrs.bind(conn)
getAddrs: () => new Promise((resolve, reject) => {
conn.getObservedAddrs((err, res) => err ? reject(err) : resolve(res))
})
}
},
restore: () => {
Expand Down

0 comments on commit 48e2beb

Please sign in to comment.