mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-14 01:31:22 +00:00
docs: auto relay example (#795)
* chore: auto relay example * chore: update examples to use process arguments * chore: add test setup for node tests and test for auto-relay * chore: apply suggestions from code review * chore: do not use promise for multiaddrs event on example
This commit is contained in:
committed by
Vasco Santos
parent
585ad52b4c
commit
4448de8432
61
examples/utils.js
Normal file
61
examples/utils.js
Normal file
@ -0,0 +1,61 @@
|
||||
'use strict'
|
||||
|
||||
const execa = require('execa')
|
||||
const fs = require('fs-extra')
|
||||
const which = require('which')
|
||||
|
||||
async function isExecutable (command) {
|
||||
try {
|
||||
await fs.access(command, fs.constants.X_OK)
|
||||
|
||||
return true
|
||||
} catch (err) {
|
||||
if (err.code === 'ENOENT') {
|
||||
return isExecutable(await which(command))
|
||||
}
|
||||
|
||||
if (err.code === 'EACCES') {
|
||||
return false
|
||||
}
|
||||
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForOutput (expectedOutput, command, args = [], opts = {}) {
|
||||
if (!await isExecutable(command)) {
|
||||
args.unshift(command)
|
||||
command = 'node'
|
||||
}
|
||||
|
||||
const proc = execa(command, args, opts)
|
||||
let output = ''
|
||||
let time = 120000
|
||||
|
||||
let timeout = setTimeout(() => {
|
||||
throw new Error(`Did not see "${expectedOutput}" in output from "${[command].concat(args).join(' ')}" after ${time/1000}s`)
|
||||
}, time)
|
||||
|
||||
proc.all.on('data', (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
output += data.toString('utf8')
|
||||
|
||||
if (output.includes(expectedOutput)) {
|
||||
clearTimeout(timeout)
|
||||
proc.kill()
|
||||
}
|
||||
})
|
||||
|
||||
try {
|
||||
await proc
|
||||
} catch (err) {
|
||||
if (!err.killed) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
waitForOutput
|
||||
}
|
Reference in New Issue
Block a user