@@ -4,6 +4,7 @@ const puppeteer = require('puppeteer');
44const path = require ( 'path' ) ;
55const child_process = require ( "child_process" ) ;
66
7+
78const files = {
89 leafletjs : fs . readFileSync ( require . resolve ( 'leaflet/dist/leaflet.js' ) , 'utf8' ) ,
910 leafletcss : fs . readFileSync ( require . resolve ( 'leaflet/dist/leaflet.css' ) , 'utf8' ) ,
@@ -16,6 +17,7 @@ const files = {
1617const templatestr = fs . readFileSync ( path . join ( __dirname , 'template.html' ) , 'utf8' )
1718const template = Handlebars . compile ( templatestr ) ;
1819
20+
1921function replacefiles ( str ) {
2022 const ff = Object . entries ( files )
2123 let res = str
@@ -24,6 +26,32 @@ function replacefiles(str) {
2426 return res
2527}
2628
29+ class Browser {
30+ /// browser singleton
31+ constructor ( ) {
32+ this . browser = null ;
33+ }
34+ async launch ( ) {
35+ return puppeteer . launch ( {
36+ args : [ "--no-sandbox" , "--disable-setuid-sandbox" ]
37+ } ) ;
38+ }
39+ async getBrowser ( ) {
40+ if ( ! this . browser || ! this . browser . isConnected ( ) ) {
41+ // console.log('NEW BROWSER')
42+ this . browser = await this . launch ( )
43+ }
44+ return this . browser
45+ }
46+ async getPage ( ) {
47+ const browser = await this . getBrowser ( )
48+ // console.log("NEW PAGE");
49+ return await browser . newPage ( )
50+ }
51+ }
52+ const browser = new Browser ( ) ;
53+
54+
2755module . exports = function ( options ) {
2856 return new Promise ( function ( resolve , reject ) {
2957 options = options || { } ;
@@ -50,64 +78,69 @@ module.exports = function(options) {
5078 }
5179
5280 ( async ( ) => {
53- const browser = await puppeteer . launch ( {
54- args : [ '--no-sandbox' , '--disable-setuid-sandbox' ]
55- } ) ;
56- const page = await browser . newPage ( ) ;
57- page . on ( 'error' , function ( err ) { reject ( err . toString ( ) ) } )
58- page . on ( 'pageerror' , function ( err ) { reject ( err . toString ( ) ) } )
59- page . on ( 'console' , function ( msg , type ) {
60- if ( msg . type === 'error' ) {
61- reject ( JSON . stringify ( msg ) )
62- }
63- } )
64- await page . setViewport ( {
65- width : Number ( options . width ) ,
66- height : Number ( options . height )
67- } ) ;
68- await page . setContent ( html , { waitUntil : 'networkidle0' } ) ;
81+ const page = await browser . getPage ( ) ;
82+ try {
83+ page . on ( 'error' , function ( err ) { reject ( err . toString ( ) ) } )
84+ page . on ( 'pageerror' , function ( err ) { reject ( err . toString ( ) ) } )
85+ page . on ( 'console' , function ( msg ) {
86+ if ( msg . type === 'error' ) {
87+ reject ( JSON . stringify ( msg ) )
88+ }
89+ } )
90+ await page . setViewport ( {
91+ width : Number ( options . width ) ,
92+ height : Number ( options . height )
93+ } ) ;
94+ await page . setContent ( html , { waitUntil : 'networkidle0' , timeout : 20000 } ) ;
6995
70- let imageBinary = await page . screenshot ( {
71- type : options . type || 'png' ,
72- quality : options . type === 'jpeg' ? Number ( options . quality || 100 ) : undefined ,
73- fullPage : true
74- } ) ;
96+ let imageBinary = await page . screenshot ( {
97+ type : options . type || 'png' ,
98+ quality : options . type === 'jpeg' ? Number ( options . quality || 100 ) : undefined ,
99+ fullPage : true
100+ } ) ;
75101
76- if ( options . imagemin ) {
77- const imagemin = require ( "imagemin" ) ;
78- const imageminJpegtran = require ( "imagemin-jpegtran" ) ;
79- const imageminOptipng = require ( "imagemin-optipng" ) ;
80- const plugins = [ ]
81- if ( options . type === 'jpeg' ) {
82- plugins . push ( imageminJpegtran ( ) ) ;
83- } else {
84- plugins . push ( imageminOptipng ( ) ) ;
85- }
86- ( async ( ) => {
87- resolve ( await imagemin . buffer (
88- imageBinary ,
89- {
90- plugins,
91- }
92- ) )
93- } ) ( ) ;
94- } else {
95- if ( options . oxipng ) {
96- const child = child_process . spawn ( '/root/.cargo/bin/oxipng' , [ '-' ] ) ;
97- child . stdin . on ( 'error' , function ( ) { } ) ;
98- child . stdin . write ( imageBinary ) ;
99- child . stdin . end ( ) ;
100- let newimg = [ ] ;
101- child . stdout . on ( 'data' , data => newimg . push ( data ) ) ;
102- child . on ( 'close' , ( ) => resolve ( Buffer . concat ( newimg ) ) ) ;
103- child . on ( 'error' , e => reject ( e . toString ( ) ) ) ;
102+ if ( options . imagemin ) {
103+ const imagemin = require ( "imagemin" ) ;
104+ const imageminJpegtran = require ( "imagemin-jpegtran" ) ;
105+ const imageminOptipng = require ( "imagemin-optipng" ) ;
106+ const plugins = [ ]
107+ if ( options . type === 'jpeg' ) {
108+ plugins . push ( imageminJpegtran ( ) ) ;
109+ } else {
110+ plugins . push ( imageminOptipng ( ) ) ;
111+ }
112+ ( async ( ) => {
113+ resolve ( await imagemin . buffer (
114+ imageBinary ,
115+ {
116+ plugins,
117+ }
118+ ) )
119+ } ) ( ) ;
104120 } else {
105- resolve ( imageBinary ) ;
121+ if ( options . oxipng ) {
122+ const child = child_process . spawn ( '/root/.cargo/bin/oxipng' , [ '-' ] ) ;
123+ child . stdin . on ( 'error' , function ( ) { } ) ;
124+ child . stdin . write ( imageBinary ) ;
125+ child . stdin . end ( ) ;
126+ let newimg = [ ] ;
127+ child . stdout . on ( 'data' , data => newimg . push ( data ) ) ;
128+ child . on ( 'close' , ( ) => resolve ( Buffer . concat ( newimg ) ) ) ;
129+ child . on ( 'error' , e => reject ( e . toString ( ) ) ) ;
130+ } else {
131+ resolve ( imageBinary ) ;
132+ }
106133 }
107- }
108134
109- browser . close ( ) ;
135+ }
136+ catch ( e ) {
137+ page . close ( ) ;
138+ // console.log("PAGE CLOSED with err" + e);
139+ throw ( e ) ;
140+ }
141+ page . close ( ) ;
142+ // console.log("PAGE CLOSED ok");
110143
111- } ) ( )
144+ } ) ( ) . catch ( reject )
112145 } ) ;
113146} ;
0 commit comments