Skip to content

Commit

Permalink
Use http server for file references in chrome.app (#472)
Browse files Browse the repository at this point in the history
* Use http server for file references in chrome.app

* fix dependencies

* Don't use static file server for lambdas

---------

Co-authored-by: Gideon Pyzer <gjpyzer@gmail.com>
  • Loading branch information
gidztech and Gideon Pyzer authored Oct 31, 2023
1 parent bde0e92 commit 16b8181
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"access": "public"
},
"dependencies": {
"mime-types": "^2.1.35",
"shelljs": "^0.8.3"
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const createChromeAWSLambdaRenderer = () => async (event) => {
}
const target = createChromeAppTarget({
baseUrl: event.baseUrl,
useStaticServer: false,
});
try {
await target.start({
Expand Down
3 changes: 2 additions & 1 deletion packages/target-chrome-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"@loki/target-chrome-core": "^0.32.0",
"chrome-launcher": "^0.14.1",
"chrome-remote-interface": "^0.32.1",
"debug": "^4.1.1"
"debug": "^4.1.1",
"find-free-port-sync": "^1.0.0"
},
"publishConfig": {
"access": "public"
Expand Down
60 changes: 59 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,64 @@
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 getStaticServerConfig(baseUrl) {
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);
}
}

return {
chromeUrl,
isLocalFile,
staticServerPath,
staticServerPort,
};
}

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

const { chromeUrl, isLocalFile, staticServerPath, staticServerPort } =
getStaticServerConfig(baseUrl);

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

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

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

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

module.exports = createChromeAppTarget;
1 change: 0 additions & 1 deletion packages/target-chrome-docker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"execa": "^5.0.0",
"find-free-port-sync": "^1.0.0",
"fs-extra": "^9.1.0",
"mime-types": "^2.1.35",
"wait-on": "^5.2.1"
},
"publishConfig": {
Expand Down
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 16b8181

Please sign in to comment.