Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use http server for file references in chrome.app #472

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
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');
gidztech marked this conversation as resolved.
Show resolved Hide resolved
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
Loading