This repository has been archived by the owner on Oct 19, 2022. 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: refactor, simplify, make handling of logs nicer
- Loading branch information
Showing
8 changed files
with
189 additions
and
162 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,5 +1,4 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
PROTOCOL_ID: '/multistream/1.0.0' | ||
} | ||
exports = module.exports | ||
exports.PROTOCOL_ID = '/multistream/1.0.0' |
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,43 @@ | ||
'use strict' | ||
|
||
const handshake = require('pull-handshake') | ||
const lp = require('pull-length-prefixed') | ||
const Connection = require('interface-connection').Connection | ||
const writeEncoded = require('../util.js').writeEncoded | ||
|
||
function selectHandler (rawConn, handlersMap, log) { | ||
const cb = (err) => { | ||
// incoming errors are irrelevant for the app | ||
log.error(err) | ||
} | ||
|
||
const stream = handshake({ timeout: 60 * 1000 }, cb) | ||
const shake = stream.handshake | ||
|
||
next() | ||
return stream | ||
|
||
function next () { | ||
lp.decodeFromReader(shake, (err, data) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
log('received:', data.toString()) | ||
const protocol = data.toString().slice(0, -1) | ||
const result = Object.keys(handlersMap).filter((id) => id === protocol) | ||
const key = result && result[0] | ||
|
||
if (key) { | ||
log('send ack back of: ' + protocol) | ||
writeEncoded(shake, data, cb) | ||
handlersMap[key](new Connection(shake.rest(), rawConn)) | ||
} else { | ||
log('not supported protocol: ' + protocol) | ||
writeEncoded(shake, new Buffer('na\n')) | ||
next() | ||
} | ||
}) | ||
} | ||
} | ||
|
||
module.exports = selectHandler |
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,35 @@ | ||
'use strict' | ||
|
||
const handshake = require('pull-handshake') | ||
const pullLP = require('pull-length-prefixed') | ||
const util = require('./util') | ||
const writeEncoded = util.writeEncoded | ||
|
||
function select (multicodec, callback, log) { | ||
const stream = handshake({ | ||
timeout: 60 * 1000 | ||
}, callback) | ||
|
||
const shake = stream.handshake | ||
|
||
log('writing multicodec: ' + multicodec) | ||
writeEncoded(shake, new Buffer(multicodec + '\n'), callback) | ||
|
||
pullLP.decodeFromReader(shake, (err, data) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
const protocol = data.toString().slice(0, -1) | ||
|
||
if (protocol !== multicodec) { | ||
return callback(new Error(`"${multicodec}" not supported`), shake.rest()) | ||
} | ||
|
||
log('received ack: ' + protocol) | ||
callback(null, shake.rest()) | ||
}) | ||
|
||
return stream | ||
} | ||
|
||
module.exports = select |
Oops, something went wrong.