Skip to content

Commit

Permalink
Use http server for file references in chrome.app
Browse files Browse the repository at this point in the history
  • Loading branch information
Gideon Pyzer committed Aug 4, 2023
1 parent 02c143c commit 2d19eb9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
File renamed without changes.
File renamed without changes.
8 changes: 7 additions & 1 deletion packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ const errors = require('./errors');
const failureHandling = require('./failure-handling');
const dependencyDetection = require('./dependency-detection');
const getAbsoluteURL = require('./get-absolute-url');
const { getLocalIPAddress } = require('./get-local-ip-address');
const { createStaticServer } = require('./create-static-server');

module.exports = Object.assign(
{ getAbsoluteURL },
{
getAbsoluteURL,
getLocalIPAddress,
createStaticServer,
},
errors,
failureHandling,
dependencyDetection
Expand Down
41 changes: 40 additions & 1 deletion packages/target-chrome-app/src/create-chrome-app-target.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
const debug = require('debug')('loki:chrome:app');
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
const getRandomPort = require('find-free-port-sync');
const {
getAbsoluteURL,
getLocalIPAddress,
createStaticServer,
} = require('@loki/core');
const { createChromeTarget } = require('@loki/target-chrome-core');

function createChromeAppTarget({
baseUrl = 'http://localhost:6006',
chromeFlags = ['--headless', '--disable-gpu', '--hide-scrollbars'],
}) {
let instance;
let staticServer;
let staticServerPath;
let staticServerPort;

let chromeUrl = getAbsoluteURL(baseUrl);
const isLocalFile = chromeUrl.indexOf('file:') === 0;

if (chromeUrl.indexOf('http://localhost') === 0 || isLocalFile) {
const ip = getLocalIPAddress();

if (!ip) {
throw new Error(
'Unable to detect local IP address, try passing --host argument'
);
}

if (isLocalFile) {
staticServerPort = getRandomPort();
staticServerPath = chromeUrl.substr('file:'.length);
chromeUrl = `http://${ip}:${staticServerPort}`;
} else {
chromeUrl = chromeUrl.replace('localhost', ip);
}
}

async function start(options = {}) {
if (isLocalFile) {
staticServer = createStaticServer(staticServerPath);
staticServer.listen(staticServerPort);
debug(`Starting static file server at ${chromeUrl}`);
}
const launchOptions = Object.assign(
{
chromeFlags,
Expand All @@ -30,6 +65,10 @@ function createChromeAppTarget({
} else {
debug('No chrome instance to kill');
}

if (staticServer) {
staticServer.close();
}
}

async function createNewDebuggerInstance() {
Expand All @@ -47,7 +86,7 @@ function createChromeAppTarget({
return client;
}

return createChromeTarget(start, stop, createNewDebuggerInstance, baseUrl);
return createChromeTarget(start, stop, createNewDebuggerInstance, chromeUrl);
}

module.exports = createChromeAppTarget;
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const {
ChromeError,
ensureDependencyAvailable,
getAbsoluteURL,
getLocalIPAddress,
createStaticServer,
} = require('@loki/core');
const { createChromeTarget } = require('@loki/target-chrome-core');
const { getLocalIPAddress } = require('./get-local-ip-address');
const { getNetworkHost } = require('./get-network-host');
const { createStaticServer } = require('./create-static-server');

const getExecutor = (dockerWithSudo) => (dockerPath, args) => {
if (dockerWithSudo) {
Expand Down

0 comments on commit 2d19eb9

Please sign in to comment.