0.1.4 • Published 6 years ago

handle-that-chrome v0.1.4

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

handle-that-chrome

handles pieces of work in chrome in parallel. A small wrapper around chrome-remote-interface.

Features:

  • chunkifys the work in pieces to minimize overhead
  • shuffles the workpieces for a better load balance

Install

npm install --save handle-that-chrome

Usage

handleThatChrome = require("handle-that-chrome")

// handleThatChrome(work:Array, options:Object)
handleThatChrome(["work1","work2"],{
  worker: (work, tab, currentIndex) => {
    // work is either ["work1"] or ["work2"]
    {DOM, CSS, Emulation, Page} = tab
    await Promise.all [DOM.enable(), CSS.enable(), Page.enable()]
    // ...
  }
}).then(=>
  // finished
)

Options

Nametypedefaultdescription
workerFunction-(required) Callback called with an array of workpieces
shuffleBooleantrueshould the work get shuffled
flattenBooleantruethe work array will be flattened
concurrencyNumber#CPUShow many workers should get spawned
onProgressFunction-will be called on progress with the remaining work count
onErrorFunction-will be called on error in your worker function
onFinishFunction-will be called once all work is done
instanceObject-you can pass a existing chrome instance, this won't be closed onFinish
chromeObject{}Launch options for chrome (https://github.com/GoogleChrome/chrome-launcher)
chrome.portNumber9222Port for the remote interface
chrome.chromeFlagsArraysee belowflags used to start chrome
// default flags
[
  "--disable-gpu"
  "--headless"
]
// plus these set by chrome-launcher
// https://github.com/GoogleChrome/chrome-launcher/blob/master/src/flags.ts

Example with ora

ora = require("ora")
handleThatChrome = require("handle-that-chrome")

spinner = ora(work.length + " workpieces remaining...").start()

handleThatChrome(work,{
  worker: => {},
  onProgress: (remaining) => { spinner.text = remaining  + " workpieces remaining..." },
  onFinish: => { spinner.succeed("finished") }
})

License

Copyright (c) 2017 Paul Pflugradt Licensed under the MIT license.