diff --git a/package.json b/package.json index dd1c466..42096fc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/sorting-hat.js b/src/sorting-hat.js index 512e917..65ca1ee 100644 --- a/src/sorting-hat.js +++ b/src/sorting-hat.js @@ -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 } diff --git a/src/wrapper.js b/src/wrapper.js index 8ae0d10..0253ca3 100644 --- a/src/wrapper.js +++ b/src/wrapper.js @@ -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: () => {