Last active
August 24, 2019 16:23
-
-
Save vasco-santos/748c49fe07a2b1d2fa85f25a093501bf to your computer and use it in GitHub Desktop.
Adapter for noise [https://github.com/ethberlinzwei/Bounties/issues/17]
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
const promiseToCallback = require('promise-to-callback') | |
const toPull = require('async-iterator-to-pull-stream') | |
const encrypt = async (localId, conn, remoteId) => { | |
// TODO implementation | |
// ... | |
pull( | |
conn, | |
toPull.duplex(handshake(state, finish)), | |
conn | |
) | |
// ... | |
} | |
module.exports = { | |
tag: '...', | |
encrypt (localId, conn, remoteId, callback) { | |
promiseToCallback(encrypt(localId, conn, remoteId))(callback) | |
}, | |
encryptAsync: encrypt | |
} |
'use strict'
const pipe = require('it-pipe')
const main = async () => {
const oneTwoThree = () => [1, 2, 3]
const collect = async (source) => {
for await (const val of source) {
console.log('val', val)
}
}
const connection = {
sink: collect,
source: oneTwoThree()
}
await pipe(['first'], connection)
await pipe(
connection,
function transform(source) {
return (async function* () { // A generator is async iterable
for await (const val of source) yield val * 2
})()
},
connection)
}
main()
'use strict'
const pipe = require('it-pipe')
const mainIniator = async () => {
const numberSource = () => [1, 2, 3, 4, 5]
const collect = async (source) => {
for await (const val of source) {
console.log('val', val)
}
}
const connection = {
sink: collect,
source: numberSource()
}
await pipe(['first message'], connection)
await pipe(
connection,
function transform(source) {
return (async function* () { // A generator is async iterable
let step = 1
for await (const val of source) {
if (step % 2 === 0) {
yield val * 2
} else {
yield step
}
step += 1
}
})()
},
connection)
}
const mainReceiver = async () => {
const numberSource = () => [10, 20, 30, 40, 50]
const collect = async (source) => {
for await (const val of source) {
console.log('val', val)
}
}
const connection = {
sink: collect,
source: numberSource()
}
await pipe(
connection,
function transform(source) {
return (async function* () { // A generator is async iterable
let step = 0
for await (const val of source) {
if (step % 2 === 0) {
yield val * 2
} else {
yield step
}
step += 1
}
})()
},
connection)
}
mainIniator()
mainReceiver()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will allow you to do all the work using promise based code, as well as async iterators inside the handshake logic 😄
After
js-libp2p
fully migrates toasync await
andasync iterators
, we would only need to change:promiseToCallback
and the exportedencryptAsync
functiontoPull.duplex
and changepull-stream
in favour ofit-pipe