11import { promises as fsPromises } from 'fs' ;
2- import { join } from 'path' ;
2+ import * as fs from 'fs' ;
3+ import * as path from 'path' ;
4+ import { promisify } from 'util' ;
5+ import * as https from 'https' ;
6+
37import * as playwright from 'playwright-core' ;
8+ import { LaunchOptions } from 'playwright-core' ;
9+
410import isLambdaRuntimeEnvironment from './util/isLambdaRuntimeEnvironment' ;
511import isHeadlessModeEnabled from './util/isHeadlessModeEnabled' ;
612import fileExists from './util/fileExists' ;
7- import setEnvironmentVariables from './util/setEnvironmentVariables' ;
8- import { LaunchOptions } from 'playwright-core' ;
13+ import getEnvironmentVariables , {
14+ AWS_FONT_DIR ,
15+ } from './util/getEnvironmentVariables' ;
916
1017const { inflate } = require ( 'lambdafs' ) ;
1118
12- setEnvironmentVariables ( ) ;
13-
1419/**
1520 * Returns a list of recommended additional Chromium flags.
1621 */
@@ -80,7 +85,7 @@ async function getChromiumExecutablePath(
8085 return '/tmp/chromium' ;
8186 }
8287
83- const input = join ( __dirname , 'bin' ) ;
88+ const input = path . join ( __dirname , 'bin' ) ;
8489 const promises = [
8590 inflate ( `${ input } /chromium.br` ) ,
8691 inflate ( `${ input } /swiftshader.tar.br` ) ,
@@ -99,12 +104,45 @@ export async function launchChromium(launchOptions?: Partial<LaunchOptions>) {
99104 const args = getChromiumArgs ( headless ) ;
100105 const executablePath = await getChromiumExecutablePath ( headless ) ;
101106
107+ const env : LaunchOptions [ 'env' ] = {
108+ ...( await getEnvironmentVariables ( ) ) ,
109+ ...( launchOptions ?. env || { } ) ,
110+ } ;
102111 const browser = await playwright . chromium . launch ( {
103112 args,
104113 executablePath,
105114 headless,
115+ env,
106116 ...launchOptions ,
107117 } ) ;
108118
109119 return browser ;
110120}
121+
122+ export const loadFont = async ( input : string ) =>
123+ new Promise ( async ( resolve , reject ) => {
124+ const url = new URL ( input ) ;
125+ const output = path . join ( AWS_FONT_DIR , url . pathname . split ( '/' ) . pop ( ) ! ) ;
126+ if ( await promisify ( fs . exists ) ( output ) ) {
127+ resolve ( ) ;
128+ return ;
129+ }
130+ if ( ! fs . existsSync ( AWS_FONT_DIR ) ) {
131+ await fsPromises . mkdir ( AWS_FONT_DIR ) ;
132+ }
133+ const stream = fs . createWriteStream ( output ) ;
134+ stream . once ( 'error' , ( error ) => {
135+ return reject ( error ) ;
136+ } ) ;
137+ https . get ( input , ( response ) => {
138+ response . on ( 'data' , ( chunk ) => {
139+ stream . write ( chunk ) ;
140+ } ) ;
141+
142+ response . once ( 'end' , ( ) => {
143+ stream . end ( ( ) => {
144+ return resolve ( ) ;
145+ } ) ;
146+ } ) ;
147+ } ) ;
148+ } ) ;
0 commit comments