@@ -3,7 +3,7 @@ const { execSync } = require('child_process');
33const execa = require ( 'execa' ) ;
44const waitOn = require ( 'wait-on' ) ;
55const CDP = require ( 'chrome-remote-interface' ) ;
6- const getRandomPort = require ( 'get- port' ) ;
6+ const getRandomPort = require ( 'find-free- port-sync ' ) ;
77const {
88 ChromeError,
99 ensureDependencyAvailable,
@@ -12,6 +12,7 @@ const {
1212const { createChromeTarget } = require ( '@loki/target-chrome-core' ) ;
1313const { getLocalIPAddress } = require ( './get-local-ip-address' ) ;
1414const { getNetworkHost } = require ( './get-network-host' ) ;
15+ const { createStaticServer } = require ( './create-static-server' ) ;
1516
1617const getExecutor = ( dockerWithSudo ) => ( dockerPath , args ) => {
1718 if ( dockerWithSudo ) {
@@ -46,16 +47,16 @@ function createChromeDockerTarget({
4647 chromeFlags = [ '--headless' , '--disable-gpu' , '--hide-scrollbars' ] ,
4748 dockerNet = null ,
4849 dockerWithSudo = false ,
49- chromeDockerUseCopy = false ,
5050 chromeDockerWithoutSeccomp = false ,
5151} ) {
52- let port ;
52+ let debuggerPort ;
53+ let staticServer ;
54+ let staticServerPath ;
55+ let staticServerPort ;
5356 let dockerId ;
5457 let host ;
55- let localPath ;
5658 let dockerUrl = getAbsoluteURL ( baseUrl ) ;
5759 const isLocalFile = dockerUrl . indexOf ( 'file:' ) === 0 ;
58- const staticMountPath = '/var/loki' ;
5960 const dockerPath = 'docker' ;
6061 const runArgs = [ 'run' , '--rm' , '-d' , '-P' ] ;
6162 const execute = getExecutor ( dockerWithSudo ) ;
@@ -65,21 +66,19 @@ function createChromeDockerTarget({
6566 }
6667 runArgs . push ( '--add-host=host.docker.internal:host-gateway' ) ;
6768
68- if ( dockerUrl . indexOf ( 'http://localhost' ) === 0 ) {
69+ if ( dockerUrl . indexOf ( 'http://localhost' ) === 0 || isLocalFile ) {
6970 const ip = getLocalIPAddress ( ) ;
7071 if ( ! ip ) {
7172 throw new Error (
7273 'Unable to detect local IP address, try passing --host argument'
7374 ) ;
7475 }
75- dockerUrl = dockerUrl . replace ( 'localhost' , ip ) ;
76- } else if ( isLocalFile ) {
77- localPath = dockerUrl . substr ( 'file:' . length ) ;
78- dockerUrl = `file://${ staticMountPath } ` ;
79- if ( ! chromeDockerUseCopy ) {
80- // setup volume mount if we're not using copy
81- runArgs . push ( '-v' ) ;
82- runArgs . push ( `${ localPath } :${ staticMountPath } ` ) ;
76+ if ( isLocalFile ) {
77+ staticServerPort = getRandomPort ( ) ;
78+ staticServerPath = dockerUrl . substr ( 'file:' . length ) ;
79+ dockerUrl = `http://${ ip } :${ staticServerPort } ` ;
80+ } else {
81+ dockerUrl = dockerUrl . replace ( 'localhost' , ip ) ;
8382 }
8483 }
8584
@@ -96,19 +95,6 @@ function createChromeDockerTarget({
9695 return stdout . trim ( ) . length !== 0 ;
9796 }
9897
99- async function copyFiles ( ) {
100- const { exitCode, stdout, stderr } = await execute ( dockerPath , [
101- 'cp' ,
102- localPath ,
103- `${ dockerId } :${ staticMountPath } ` ,
104- ] ) ;
105-
106- if ( exitCode !== 0 ) {
107- throw new Error ( `Failed to copy files, ${ stderr } ` ) ;
108- }
109- return stdout . trim ( ) . length !== 0 ;
110- }
111-
11298 async function ensureImageDownloaded ( ) {
11399 ensureDependencyAvailable ( 'docker' ) ;
114100
@@ -119,13 +105,19 @@ function createChromeDockerTarget({
119105 }
120106
121107 async function start ( ) {
122- port = await getRandomPort ( ) ;
123-
124108 ensureDependencyAvailable ( 'docker' ) ;
109+
110+ debuggerPort = getRandomPort ( ) ;
111+ if ( isLocalFile ) {
112+ staticServer = createStaticServer ( staticServerPath ) ;
113+ staticServer . listen ( staticServerPort ) ;
114+ debug ( `Starting static file server at ${ dockerUrl } ` ) ;
115+ }
116+
125117 const dockerArgs = runArgs . concat ( [
126118 '--shm-size=1g' ,
127119 '-p' ,
128- `${ port } :${ port } ` ,
120+ `${ debuggerPort } :${ debuggerPort } ` ,
129121 ] ) ;
130122
131123 if ( dockerNet ) {
@@ -139,7 +131,7 @@ function createChromeDockerTarget({
139131 '--no-first-run' ,
140132 '--disable-extensions' ,
141133 '--remote-debugging-address=0.0.0.0' ,
142- `--remote-debugging-port=${ port } ` ,
134+ `--remote-debugging-port=${ debuggerPort } ` ,
143135 ] )
144136 . concat ( chromeFlags ) ;
145137
@@ -151,9 +143,6 @@ function createChromeDockerTarget({
151143 const { exitCode, stdout, stderr } = await execute ( dockerPath , args ) ;
152144 if ( exitCode === 0 ) {
153145 dockerId = stdout ;
154- if ( chromeDockerUseCopy ) {
155- await copyFiles ( ) ;
156- }
157146 const logs = execute ( dockerPath , [ 'logs' , dockerId , '--follow' ] ) ;
158147 const errorLogs = [ ] ;
159148 logs . stderr . on ( 'data' , ( chunk ) => {
@@ -162,7 +151,7 @@ function createChromeDockerTarget({
162151
163152 host = await getNetworkHost ( execute , dockerId ) ;
164153 try {
165- await waitOnCDPAvailable ( host , port ) ;
154+ await waitOnCDPAvailable ( host , debuggerPort ) ;
166155 } catch ( error ) {
167156 if (
168157 error . message . startsWith ( 'Timed out waiting for' ) &&
@@ -195,17 +184,20 @@ function createChromeDockerTarget({
195184 } else {
196185 debug ( 'No chrome docker instance to kill' ) ;
197186 }
187+ if ( staticServer ) {
188+ staticServer . close ( ) ;
189+ }
198190 }
199191
200192 async function createNewDebuggerInstance ( ) {
201- debug ( `Launching new tab with debugger at port ${ host } :${ port } ` ) ;
202- const target = await CDP . New ( { host, port } ) ;
193+ debug ( `Launching new tab with debugger at port ${ host } :${ debuggerPort } ` ) ;
194+ const target = await CDP . New ( { host, port : debuggerPort } ) ;
203195 debug ( `Launched with target id ${ target . id } ` ) ;
204- const client = await CDP ( { host, port, target } ) ;
196+ const client = await CDP ( { host, port : debuggerPort , target } ) ;
205197
206198 client . close = ( ) => {
207199 debug ( 'Closing tab' ) ;
208- return CDP . Close ( { host, port, id : target . id } ) ;
200+ return CDP . Close ( { host, port : debuggerPort , id : target . id } ) ;
209201 } ;
210202
211203 return client ;
0 commit comments