forkfeed

Passes Answer Values To A Child Process On Data.

Usage no npm install needed!

<script type="module">
  import forkfeed from 'https://cdn.skypack.dev/forkfeed';
</script>

README

ForkFeed

npm version Pipeline Badge

forkfeed is a function that passes answer values to a child process on data. For example, if a fork was spawned which requires user interaction, this module can be used to pass answers to it according to the seen data from stdout.

yarn add forkfeed
npm i forkfeed

Table Of Contents

API

The package is available by importing its default function:

import forkFeed from 'forkfeed'

forkfeed(
  readable: !stream.Readable,
  stdin: !stream.Writable,
  inputs: !Array<!Array<(!RegExp|string)>>,
  log=: stream.Writable,
  eol=: string,
): void

Write data to the writable when data from the readable matches the regexp.

  • readable* Node.JS Docs!stream.Readable: A readable stream to detect data on.
  • stdin* Node.JS Docs!stream.Writable: A writable stream to pass answers to.
  • inputs* !Array<!Array<(!RegExp | string)>>: A serial collection of answers. Each answer will be ended with an EOL character (unless specified otherwise in the last argument). For example, pass [[/question/, 'answer'], [/question2/, 'answer2]].
  • log Node.JS Docsstream.Writable (optional): A stream to which to write both data from readable, and the passed answer. Default null.
  • eol string (optional): The line feed to pass at the end of each answer. By default, ForkFeed uses os.EOL, and when readline interface is used, there won't be \r in answers on Windows, but this can be useful for logging.

Sets up a listener on the Readable stream and writes answers to the Writable stream when data specified in inputs was detected. The logging stream will receive both data and answers.

Given a fork source code as

const rl = require('readline')

const i = rl.createInterface({
  input: process.stdin,
  output: process.stderr,
})
i.question(
  'What was the football coach yelling at the vending machine?\n> ',
  () => {
    i.question('What do snowmen do in their spare time?\n> ', () => {
      process.exit(1)
    })
  })

The function can be used in the following manner:

/* yarn example/ */
import forkFeed from 'forkfeed'
import { fork } from 'child_process'

(async () => {
  const cp = fork('example/fork', [], { stdio: 'pipe' })
  forkFeed(cp.stderr, cp.stdin, [
    [/coach/, 'Gimme my quarter back!!!'],
    [/snowmen/, 'Just chilling.'],
  ], process.stdout)
})()
What was the football coach yelling at the vending machine?
> Gimme my quarter back!!!
What do snowmen do in their spare time?
> Just chilling.

Copyright & License

GNU Affero General Public License v3.0

Art Deco © Art Deco™ for ContextTesting 2020 ContextTesting AGPL-3.0