forked from frinyvonnick/node-html-to-image
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support an instance mode for node-html-to-image
**This is a breaking API change (for now)** node-html-to-image's default export is now a function which returns an instance object with a render() method which is the old nodeHtmlToImage() functionality. The reason for doing this is to allow us to instantiate a single 'image maker' object that we call render() on multiple times and reuse the same puppeteer cluster. In addition, * Update the type definitions. * Use Cluster.execute() instead of Cluster.queue() plus Cluster.idle() as this will allow better use in concurrent situations. We will only wait for our jobs to finish rather than for the entire cluster to go idle. Fixes frinyvonnick#80
- Loading branch information
Showing
3 changed files
with
54 additions
and
34 deletions.
There are no files selected for viewing
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,45 +1,57 @@ | ||
const puppeteer = require('puppeteer') | ||
const { Cluster } = require('puppeteer-cluster') | ||
|
||
const { makeScreenshot } = require('./screenshot.js') | ||
|
||
module.exports = async function(options) { | ||
module.exports = function(options) { | ||
let instance = {}; | ||
|
||
const { | ||
html, | ||
content, | ||
output, | ||
puppeteerArgs = {}, | ||
} = options | ||
} = options; | ||
|
||
if (!html) { | ||
throw Error('You must provide an html property.') | ||
} | ||
|
||
const cluster = await Cluster.launch({ | ||
const _cp = Cluster.launch({ | ||
concurrency: Cluster.CONCURRENCY_CONTEXT, | ||
maxConcurrency: 2, | ||
puppeteerOptions: { ...puppeteerArgs, headless: true }, | ||
}); | ||
|
||
let buffers = [] | ||
instance.shutdown = async () => { | ||
const _cluster = await _cp; | ||
await _cluster.close(); | ||
} | ||
|
||
instance.render = async (renderOpts) => { | ||
const { | ||
html, | ||
content, | ||
output, | ||
} = renderOpts; | ||
|
||
await cluster.task(async ({ page, data: { content, output } }) => { | ||
const buffer = await makeScreenshot(page, { ...options, content, output }) | ||
if (!html) { | ||
throw Error('You must provide an html property.') | ||
} | ||
|
||
buffers.push(buffer); | ||
}); | ||
let buffers = []; | ||
|
||
const shouldBatch = Array.isArray(content) | ||
const contents = shouldBatch ? content : [{ ...content, output }] | ||
const _cluster = await _cp; | ||
await _cluster.task(async ({ page, data: { content, output } }) => { | ||
const buffer = await makeScreenshot(page, { ...renderOpts, content, output }) | ||
buffers.push(buffer); | ||
}); | ||
|
||
contents.forEach(content => { | ||
const { output, ...pageContent } = content | ||
cluster.queue({ output, content: pageContent }) | ||
}) | ||
const shouldBatch = Array.isArray(content) | ||
const contents = shouldBatch ? content : [{ ...content, output }] | ||
|
||
await cluster.idle(); | ||
await cluster.close(); | ||
let promises = []; | ||
|
||
return shouldBatch ? buffers : buffers[0] | ||
} | ||
contents.forEach(content => { | ||
const { output, ...pageContent } = content; | ||
promises.push(_cluster.execute({ output, content: pageContent })); | ||
}) | ||
|
||
await Promise.all(promises); | ||
return shouldBatch ? buffers : buffers[0]; | ||
}; | ||
|
||
return instance; | ||
} |
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,11 +1,16 @@ | ||
/// <reference types="node" /> | ||
|
||
import { NodeHtmlToImageOptions } from './options'; | ||
import { NodeHtmlToImageConstructorOptions, NodeHtmlToImageOptions } from './options'; | ||
|
||
/** | ||
* `node-html-to-image` takes a screenshot of the body tag's content. | ||
* @param options Options to pass to the generatorr | ||
*/ | ||
declare function nodeHtmlToImage(options: NodeHtmlToImageOptions): Promise<string | Buffer | (string | Buffer)[]>; | ||
declare function nodeHtmlToImage(options: NodeHtmlToImageConstructorOptions): NodeHtmlToImageInstance; | ||
|
||
export interface NodeHtmlToImageInstance { | ||
render(options: NodeHtmlToImageOptions): Promise<string | Buffer | (string | Buffer)[]>; | ||
} | ||
|
||
export default nodeHtmlToImage; | ||
export * from './options'; | ||
export * from './options'; |
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