Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/473-fix-chrome-docker-host-access
Browse files Browse the repository at this point in the history
  • Loading branch information
oblador authored Oct 31, 2023
2 parents 6815cce + dd1665c commit 65129bc
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 10 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
3 changes: 2 additions & 1 deletion packages/integration-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
],
"main": "src/index.js",
"dependencies": {
"@loki/integration-core": "^0.32.0"
"@loki/integration-core": "^0.32.0",
"hoist-non-react-statics": "*"
},
"peerDependencies": {
"@storybook/addons": "^5 || ^6",
Expand Down
2 changes: 1 addition & 1 deletion packages/loki/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@loki/target-native-ios-simulator": "^0.32.0"
},
"peerDependencies": {
"@storybook/react": "^5 || ^6"
"@storybook/react": "^5 || ^6 || ^7"
},
"peerDependenciesMeta": {
"@storybook/react": {
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
14 changes: 10 additions & 4 deletions packages/target-chrome-docker/src/create-chrome-docker-target.js
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 Expand Up @@ -43,7 +43,7 @@ const waitOnCDPAvailable = (host, port) =>

function createChromeDockerTarget({
baseUrl = 'http://localhost:6006',
chromeDockerImage = 'yukinying/chrome-headless-browser-stable',
chromeDockerImage = 'yukinying/chrome-headless-browser-stable:100.0.4896.127',
chromeFlags = ['--headless', '--disable-gpu', '--hide-scrollbars'],
dockerNet = null,
dockerWithSudo = false,
Expand Down Expand Up @@ -179,7 +179,13 @@ function createChromeDockerTarget({
async function stop() {
if (dockerId) {
debug(`Killing chrome docker instance with id ${dockerId}`);
await execute(dockerPath, ['kill', dockerId]);
try {
await execute(dockerPath, ['kill', dockerId]);
} catch (e) {
if (e.toString().indexOf('No such container') === -1) {
throw e;
}
}
} else {
debug('No chrome docker instance to kill');
}
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11837,6 +11837,13 @@ hmac-drbg@^1.0.0:
minimalistic-assert "^1.0.0"
minimalistic-crypto-utils "^1.0.1"

hoist-non-react-statics@*:
version "3.3.2"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
dependencies:
react-is "^16.7.0"

hoist-non-react-statics@^3.3.0:
version "3.3.1"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz#101685d3aff3b23ea213163f6e8e12f4f111e19f"
Expand Down

0 comments on commit 65129bc

Please sign in to comment.