Skip to content

Commit

Permalink
Merge pull request #5 from isleofcode/release/0.2.0
Browse files Browse the repository at this point in the history
Release/0.2.0
  • Loading branch information
anulman authored Oct 18, 2017
2 parents f6cd9a4 + e8cdc11 commit 9f5865a
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.eslintcache
.npm

dist
node_modules

*.log
Expand Down
2 changes: 1 addition & 1 deletion _docs
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "render-vendor",
"version": "0.1.3",
"version": "0.2.0",
"description": "The fastest way to render HTML documents to PDFs",
"engines": {
"node": ">=4.0.0"
Expand Down Expand Up @@ -49,6 +49,7 @@
"listr": "^0.12.0",
"lodash": "^4.17.4",
"phantomjs-prebuilt": "^2.1.14",
"puppeteer": "^0.12.0",
"request": "^2.81.0",
"rsvp": "^4.0.1"
},
Expand Down
6 changes: 4 additions & 2 deletions src/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ export class Page {
async render(options = {}, ...args) {
assert.ok(!this.isDestroyed && !this.isDestroying && this.isLoaded);

if (isString(options)) {
let pagePathParamKey = this.renderer.constructor.pagePathParamKey;

if (isString(pagePathParamKey) && isString(options)) {
let filename = options;

options = isPresent(args[0]) ? args[0] : {};
options.filename = filename;
options[pagePathParamKey] = filename;
}

if (isString(options.filename) && !isString(options.format)) {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let defaultRenderer;

export class Renderer extends EventEmitter {
static get rendererConstructor() {
return this._rendererConstructor || require('./renderers/phantom').default;
return this._rendererConstructor || require('./renderers/chrome').default;
}

static set rendererConstructor(value) {
Expand Down
154 changes: 154 additions & 0 deletions src/renderers/chrome/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import _ from 'lodash';
import path from 'path';
import puppeteer from 'puppeteer';

import Renderer from '../../renderer';

const { isString } = _;

const DEFAULT_BOOT_OPTIONS = {}; // use puppeteer defaults, https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions

export class ChromeRenderer extends Renderer {
static get DEFAULT_BOOT_OPTIONS() {
return this._DEFAULT_BOOT_OPTIONS || DEFAULT_BOOT_OPTIONS;
}

static set DEFAULT_BOOT_OPTIONS(value) {
this._DEFAULT_BOOT_OPTIONS = value;
}

static get pagePathParamKey() {
return 'path';
}

constructor(attrs = {}) {
super(attrs);

this.bootOptions = this.bootOptions ||
this.constructor.DEFAULT_BOOT_OPTIONS;
}

get browser() {
return this._browser;
}

set browser(value) {
this._browser = value;
}

get bootOptions() {
return this._bootOptions;
}

set bootOptions(value) {
this._bootOptions = value;
}

async boot() {
if (this.isBooted) {
return;
}

try {
if (!this.isBooting) {
let bootOptions = this.bootOptions;

this.isBooting = true;
this.browser = bootOptions.hasOwnProperty('browserWSEndpoint') ?
await puppeteer.connect(bootOptions) :
await puppeteer.launch(bootOptions);

this.isBooted = true;
}
} catch (err) {
this.isBooted = false;
throw err;
}
}

async shutdown() {
let browser = this.browser;

if (browser !== null && browser !== undefined) {
browser.close();
this.browser = undefined;
}

this.isBooted = false;
}

async ping() {
return this.browser.pages();
}

async loadPage(page, options) {
let chromePage = page._chromePage;

if (chromePage === null || chromePage === undefined) {
chromePage = await this.browser.newPage();
page._chromePage = chromePage;
}

page.isLoading = true;

try {
let { url, html } = options;

if (html !== undefined && html !== null) {
await chromePage.setContent(html);
} else {
if (url !== undefined && url !== null) {
delete options.url;
} else {
url = page.id;
}

await chromePage.goto(url, options);
}

page.isLoaded = true;
} catch (err) {
page.isLoaded = false;
throw err;
}
}

async renderPage(page, options = {}) {
let { path: filepath, type } = options;
let buffer;

if (!isString(type)) {
type = isString(filepath) ? path.extname(filepath).slice(1) : 'html';
type = type.length > 0 ? type : 'html';
}

switch (type) {
case 'html':
buffer = await page._chromePage.content();
break;
case 'pdf':
let { emulateMedia } = options;

if (emulateMedia !== null && emulateMedia !== undefined) {
await page._chromePage.emulateMedia(emulateMedia);
delete options.emulateMedia;
}

buffer = await page._chromePage.pdf(options);
break;
default:
buffer = await page._chromePage.screenshot(options);
break;
}

return isString(filepath) ? filepath : buffer;
}

async unloadPage(page, ...args) {
await page._chromePage.close();

return true;
}
}

export default ChromeRenderer;
4 changes: 4 additions & 0 deletions src/renderers/phantom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export class PhantomRenderer extends Renderer {
return denodeify(request);
}

static get pagePathParamKey() {
return 'filename';
}

constructor(attrs = {}) {
super(attrs);

Expand Down
Loading

0 comments on commit 9f5865a

Please sign in to comment.