Skip to content

Commit

Permalink
[0.5.0] Docs updated + Ability to not specify callbackName
Browse files Browse the repository at this point in the history
  • Loading branch information
ashubham committed Jan 15, 2018
1 parent f2a5e7f commit 86dd682
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 21 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

<img src="https://github.com/ashubham/webshot-factory/raw/master/assets/webshot-factory.png" align="right" alt="Webshot Factory" />

screenshots at scale based on headless chrome
screenshots at scale based on headless chrome.

## Basic Concept

- `Webshot-factory` creates a number of headless-chrome worker instances which take screenshots in round robin. Thus, can be horizontally scaled to provide good throughput.
- Includes a debug status page to monitor the worker instances.
- Can be used for batch report generation.
- Or to take a number screenshots in general.

## Installation

Expand All @@ -25,7 +32,8 @@ await shotFactory.init({
// The callback method to be exposed on the window,
// which would be called by the application
// Shot will be taken when callback is called.
callbackName: 'callPhantom',
// This was 'callPhantom' in PhantomJS.
callbackName: '',

// A cache warmer url,
// so that workers can cache the webpage scripts.
Expand All @@ -40,7 +48,13 @@ await shotFactory.init({
// a shot will be scheduled on a worker
// chrome instance.
shotFactory.getShot('http://yahoo.com').then(buffer => {
// Do whatever with the buffer, can be used to upload.
console.log(buffer);
// Or can be saved to a file.
// Using the `fs` module.
fs.createWriteStream('shot.png')
.write(buffer)
.end();
});
```

Expand All @@ -52,4 +66,6 @@ Visit: `http://<host>:<webshotDebugPort>/status`

To check the status and debug any problems. The page looks like this:

<img src="https://github.com/ashubham/webshot-factory/raw/master/assets/webshot-debug-page.png" alt="Webshot Factory status" />


Binary file added assets/webshot-debug-page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 7 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ process.env.webshotDebugPort = process.env.webshotDebugPort || '3030';
Logger.setGlobalLogLevel(process.env.loglevel);
let app = express();
let ip;
let _logger = Logger.getLogger("index");

internalIp.v4().then(_ip => ip = _ip);

export interface Config extends shotPool.PoolConfig {
webshotDebugPort: string;
webshotDebugPort?: string;
}

let defaultConfig: Config = {
concurrency: 10,
callbackName: 'callPhantom',
callbackName: '',
warmerUrl: '',
width: 800,
height: 600,
Expand All @@ -32,6 +34,8 @@ export async function init(options: Config) {
await shotPool.create(
options
);
app.listen(parseInt(passedConfig.webshotDebugPort), ip);
_logger.info('Listening on debug port', passedConfig.webshotDebugPort, ip);
return shotPool;
}

Expand Down Expand Up @@ -88,6 +92,4 @@ app.get('/status', (req, res) => {
</body>
</html>
`)
});

app.listen(parseInt(passedConfig.webshotDebugPort), ip);
});
40 changes: 28 additions & 12 deletions lib/shot-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as _ from 'lodash';
import * as puppeteer from 'puppeteer';
import * as Logger from 'log4js';
import * as P from 'bluebird';
import { config } from 'bluebird';

let _logger = Logger.getLogger("shot-worker");
const DEBUG_PORT_OFFSET = 9201;
Expand All @@ -22,12 +23,14 @@ export class ShotWorker {
private timeout: number = 60000;
private shotCallback: (err, buffer: Buffer) => void = _.noop;
private isBusy: boolean = true;
public config: WorkerConfig;
constructor(public id: number) {

}

public static async create(idx: number, config: WorkerConfig) {
let worker = new ShotWorker(idx);
worker.config = config;
await worker.init(idx, config.callbackName, config.warmerUrl, config.width, config.height, config.timeout);
return worker;
}
Expand All @@ -47,7 +50,18 @@ export class ShotWorker {
}
resolve(buffer);
};
this.page.goto(url, { waitUntil: 'networkidle' });
this.page.goto(url, {
waitUntil: 'networkidle2'
}).then(async () => {
if(!this.config.callbackName) {
let buffer = await this.page.screenshot({
fullPage: true
});
this.shotCallback(null, buffer);
}
}).then(null, (err) => {
this.shotCallback(err, null);
});
})
.timeout(this.timeout)
.finally(() => {
Expand All @@ -74,7 +88,7 @@ export class ShotWorker {
}

private async init(idx: number = 0,
callbackName: string = 'callPhantom',
callbackName: string = '',
warmerUrl: string = '',
width: number = 800,
height: number = 600,
Expand All @@ -101,19 +115,21 @@ export class ShotWorker {
});

// Define a window.onCustomEvent function on the page.
await this.page.exposeFunction(callbackName, e => {
_logger.debug('Callback called from browser with', e);
return this.page.screenshot({
fullPage: true
}).then((buffer: Buffer) => {
this.shotCallback(null, buffer);
}, (err) => {
this.shotCallback(err, null);
if(callbackName) {
await this.page.exposeFunction(callbackName, e => {
_logger.debug('Callback called from browser with', e);
return this.page.screenshot({
fullPage: true
}).then((buffer: Buffer) => {
this.shotCallback(null, buffer);
}, (err) => {
this.shotCallback(err, null);
});
});
});
}

if (warmerUrl) {
await this.page.goto(warmerUrl, { waitUntil: 'networkidle' });
await this.page.goto(warmerUrl, { waitUntil: 'networkidle2' });
}

_logger.info(`Worker ${idx} ready`);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webshot-factory",
"version": "0.3.3",
"version": "0.5.0",
"description": "screenshots at scale based on headless chrome",
"main": "dist/index.js",
"types": "dist/declarations/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion test/test-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as shotFactory from '../index';

shotFactory.init({
concurrency: 5,
callbackName: 'callPhantom',
callbackName: '',
warmerUrl: 'http://google.com',
width: 1000, // shot width
height: 600 // shot height
Expand Down

0 comments on commit 86dd682

Please sign in to comment.