Skip to content

Commit 65129bc

Browse files
authored
Merge branch 'master' into bugfix/473-fix-chrome-docker-host-access
2 parents 6815cce + dd1665c commit 65129bc

File tree

12 files changed

+90
-10
lines changed

12 files changed

+90
-10
lines changed

packages/core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"access": "public"
2424
},
2525
"dependencies": {
26+
"mime-types": "^2.1.35",
2627
"shelljs": "^0.8.3"
2728
}
2829
}

packages/core/src/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ const errors = require('./errors');
22
const failureHandling = require('./failure-handling');
33
const dependencyDetection = require('./dependency-detection');
44
const getAbsoluteURL = require('./get-absolute-url');
5+
const { getLocalIPAddress } = require('./get-local-ip-address');
6+
const { createStaticServer } = require('./create-static-server');
57

68
module.exports = Object.assign(
7-
{ getAbsoluteURL },
9+
{
10+
getAbsoluteURL,
11+
getLocalIPAddress,
12+
createStaticServer,
13+
},
814
errors,
915
failureHandling,
1016
dependencyDetection

packages/integration-react-native/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
],
2121
"main": "src/index.js",
2222
"dependencies": {
23-
"@loki/integration-core": "^0.32.0"
23+
"@loki/integration-core": "^0.32.0",
24+
"hoist-non-react-statics": "*"
2425
},
2526
"peerDependencies": {
2627
"@storybook/addons": "^5 || ^6",

packages/loki/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"@loki/target-native-ios-simulator": "^0.32.0"
5555
},
5656
"peerDependencies": {
57-
"@storybook/react": "^5 || ^6"
57+
"@storybook/react": "^5 || ^6 || ^7"
5858
},
5959
"peerDependenciesMeta": {
6060
"@storybook/react": {

packages/renderer-aws-lambda/src/create-aws-lambda-renderer.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const createChromeAWSLambdaRenderer = () => async (event) => {
5454
}
5555
const target = createChromeAppTarget({
5656
baseUrl: event.baseUrl,
57+
useStaticServer: false,
5758
});
5859
try {
5960
await target.start({

packages/target-chrome-app/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"@loki/target-chrome-core": "^0.32.0",
2424
"chrome-launcher": "^0.14.1",
2525
"chrome-remote-interface": "^0.32.1",
26-
"debug": "^4.1.1"
26+
"debug": "^4.1.1",
27+
"find-free-port-sync": "^1.0.0"
2728
},
2829
"publishConfig": {
2930
"access": "public"

packages/target-chrome-app/src/create-chrome-app-target.js

+59-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,64 @@
11
const debug = require('debug')('loki:chrome:app');
22
const chromeLauncher = require('chrome-launcher');
33
const CDP = require('chrome-remote-interface');
4+
const getRandomPort = require('find-free-port-sync');
5+
const {
6+
getAbsoluteURL,
7+
getLocalIPAddress,
8+
createStaticServer,
9+
} = require('@loki/core');
410
const { createChromeTarget } = require('@loki/target-chrome-core');
511

12+
function getStaticServerConfig(baseUrl) {
13+
let staticServerPath;
14+
let staticServerPort;
15+
16+
let chromeUrl = getAbsoluteURL(baseUrl);
17+
const isLocalFile = chromeUrl.indexOf('file:') === 0;
18+
19+
if (chromeUrl.indexOf('http://localhost') === 0 || isLocalFile) {
20+
const ip = getLocalIPAddress();
21+
22+
if (!ip) {
23+
throw new Error(
24+
'Unable to detect local IP address, try passing --host argument'
25+
);
26+
}
27+
28+
if (isLocalFile) {
29+
staticServerPort = getRandomPort();
30+
staticServerPath = chromeUrl.substr('file:'.length);
31+
chromeUrl = `http://${ip}:${staticServerPort}`;
32+
} else {
33+
chromeUrl = chromeUrl.replace('localhost', ip);
34+
}
35+
}
36+
37+
return {
38+
chromeUrl,
39+
isLocalFile,
40+
staticServerPath,
41+
staticServerPort,
42+
};
43+
}
44+
645
function createChromeAppTarget({
746
baseUrl = 'http://localhost:6006',
47+
useStaticServer = true,
848
chromeFlags = ['--headless', '--disable-gpu', '--hide-scrollbars'],
949
}) {
1050
let instance;
51+
let staticServer;
52+
53+
const { chromeUrl, isLocalFile, staticServerPath, staticServerPort } =
54+
getStaticServerConfig(baseUrl);
1155

1256
async function start(options = {}) {
57+
if (useStaticServer && isLocalFile) {
58+
staticServer = createStaticServer(staticServerPath);
59+
staticServer.listen(staticServerPort);
60+
debug(`Starting static file server at ${chromeUrl}`);
61+
}
1362
const launchOptions = Object.assign(
1463
{
1564
chromeFlags,
@@ -30,6 +79,10 @@ function createChromeAppTarget({
3079
} else {
3180
debug('No chrome instance to kill');
3281
}
82+
83+
if (useStaticServer && staticServer) {
84+
staticServer.close();
85+
}
3386
}
3487

3588
async function createNewDebuggerInstance() {
@@ -47,7 +100,12 @@ function createChromeAppTarget({
47100
return client;
48101
}
49102

50-
return createChromeTarget(start, stop, createNewDebuggerInstance, baseUrl);
103+
return createChromeTarget(
104+
start,
105+
stop,
106+
createNewDebuggerInstance,
107+
useStaticServer ? chromeUrl : baseUrl
108+
);
51109
}
52110

53111
module.exports = createChromeAppTarget;

packages/target-chrome-docker/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"execa": "^5.0.0",
2828
"find-free-port-sync": "^1.0.0",
2929
"fs-extra": "^9.1.0",
30-
"mime-types": "^2.1.35",
3130
"wait-on": "^5.2.1"
3231
},
3332
"publishConfig": {

packages/target-chrome-docker/src/create-chrome-docker-target.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ const {
88
ChromeError,
99
ensureDependencyAvailable,
1010
getAbsoluteURL,
11+
getLocalIPAddress,
12+
createStaticServer,
1113
} = require('@loki/core');
1214
const { createChromeTarget } = require('@loki/target-chrome-core');
13-
const { getLocalIPAddress } = require('./get-local-ip-address');
1415
const { getNetworkHost } = require('./get-network-host');
15-
const { createStaticServer } = require('./create-static-server');
1616

1717
const getExecutor = (dockerWithSudo) => (dockerPath, args) => {
1818
if (dockerWithSudo) {
@@ -43,7 +43,7 @@ const waitOnCDPAvailable = (host, port) =>
4343

4444
function createChromeDockerTarget({
4545
baseUrl = 'http://localhost:6006',
46-
chromeDockerImage = 'yukinying/chrome-headless-browser-stable',
46+
chromeDockerImage = 'yukinying/chrome-headless-browser-stable:100.0.4896.127',
4747
chromeFlags = ['--headless', '--disable-gpu', '--hide-scrollbars'],
4848
dockerNet = null,
4949
dockerWithSudo = false,
@@ -179,7 +179,13 @@ function createChromeDockerTarget({
179179
async function stop() {
180180
if (dockerId) {
181181
debug(`Killing chrome docker instance with id ${dockerId}`);
182-
await execute(dockerPath, ['kill', dockerId]);
182+
try {
183+
await execute(dockerPath, ['kill', dockerId]);
184+
} catch (e) {
185+
if (e.toString().indexOf('No such container') === -1) {
186+
throw e;
187+
}
188+
}
183189
} else {
184190
debug('No chrome docker instance to kill');
185191
}

yarn.lock

+7
Original file line numberDiff line numberDiff line change
@@ -11837,6 +11837,13 @@ hmac-drbg@^1.0.0:
1183711837
minimalistic-assert "^1.0.0"
1183811838
minimalistic-crypto-utils "^1.0.1"
1183911839

11840+
hoist-non-react-statics@*:
11841+
version "3.3.2"
11842+
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
11843+
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
11844+
dependencies:
11845+
react-is "^16.7.0"
11846+
1184011847
hoist-non-react-statics@^3.3.0:
1184111848
version "3.3.1"
1184211849
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz#101685d3aff3b23ea213163f6e8e12f4f111e19f"

0 commit comments

Comments
 (0)