Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// TODO: remove other listeners
resolve(ws);
ws.on('message', onMessage);
};
const onClose = () => {
debug('Connection closed');
clearTimeout(timeout);
};
ws.on('open', onOpen);
ws.on('close', onClose);
ws.on('error', onError);
});
const prepare = withRetries(5)(async () => {
sendLokiCommand('prepare');
await waitForLokiMessage('didPrepare');
});
async function start() {
try {
socket = await connect(socketUri);
} catch (err) {
throw new Error(
'Failed connecting to storybook server. Start it with `yarn storybook` and review --react-native-port and --host arguments.'
);
}
try {
await prepare();
} catch (err) {
throw new Error(
const maybeFulfillPromise = () => {
if (pageLoaded && Object.keys(pendingRequestURLMap).length === 0) {
if (failedURLs.length !== 0) {
reject(new FetchingURLsError(failedURLs));
} else {
// In some cases such as fonts further requests will only happen after the page has been fully rendered
if (stabilizationTimer) {
clearTimeout(stabilizationTimer);
}
stabilizationTimer = setTimeout(
resolve,
REQUEST_STABILIZATION_TIMEOUT
);
}
}
};
return mapLimit(event.stories, concurrency, async task => {
try {
const screenshot = await captureScreenshotForStory(target, {
kind: task.kind,
story: task.story,
configuration: task.configuration,
options: event.options,
});
return screenshot;
} catch (error) {
return { errorMessage: serializeError(unwrapError(error)) };
}
});
};
const waitForLokiMessage = async (type, timeout = 2000) => {
const prefixedType = `${MESSAGE_PREFIX}${type}`;
const matchesPlatform = data => data && data.platform === platform;
try {
const message = await withTimeout(timeout)(
messageQueue.waitFor(prefixedType, matchesPlatform)
);
return message;
} catch (err) {
messageQueue.rejectAllOfType(prefixedType);
throw err;
}
};
const buffer = Buffer.from(screenshot.data, 'base64');
return buffer;
}
)
);
return client;
}
const getStoryUrl = (kind, story) =>
`${baseUrl}/iframe.html?selectedKind=${encodeURIComponent(
kind
)}&selectedStory=${encodeURIComponent(story)}`;
const launchStoriesTab = withTimeout(LOADING_STORIES_TIMEOUT)(
withRetries(2)(async url => {
const tab = await launchNewTab({
width: 100,
height: 100,
chromeEnableAnimations: true,
clearBrowserCookies: false,
});
await tab.loadUrl(url);
return tab;
})
);
async function getStorybook() {
const url = `${baseUrl}/iframe.html`;
try {
const tab = await launchStoriesTab(url);
const getPositionInViewport = async selector => {
try {
return await executeFunctionWithWindow(getSelectorBoxSize, selector);
} catch (error) {
if (error.message === 'No visible elements found') {
throw new Error(
`Unable to get position of selector "${selector}". Review the \`chromeSelector\` option and make sure your story doesn't crash.`
);
}
throw error;
}
};
client.captureScreenshot = withRetries(options.chromeRetries)(
withTimeout(CAPTURING_SCREENSHOT_TIMEOUT, 'captureScreenshot')(
async (selector = 'body') => {
debug(`Getting viewport position of "${selector}"`);
const position = await getPositionInViewport(selector);
if (position.width === 0 || position.height === 0) {
throw new Error(
`Selector "${selector} has zero width or height. Can't capture screenshot.`
);
}
const clip = {
scale: 1,
x: Math.floor(position.x),
y: Math.floor(position.y),
width: Math.ceil(position.width),
height: Math.ceil(position.height),
await prepare();
lastStoryCrashed = false;
}
const storyId = toId(kind, story);
debug('captureScreenshotForStory', kind, story, storyId);
send('setCurrentStory', { kind, story, storyId });
try {
await waitForLokiMessage('ready', 30000);
} catch (error) {
if (error instanceof NativeError) {
lastStoryCrashed = error.isFatal;
}
throw error;
}
await withTimeout(10000)(saveScreenshotToFile(outputPath));
}
{ media: options.chromeEmulatedMedia },
configuration
);
if (configuration.preset) {
if (!presets[configuration.preset]) {
throw new Error(`Invalid preset ${configuration.preset}`);
}
tabOptions = Object.assign(tabOptions, presets[configuration.preset]);
}
const selector = configuration.chromeSelector || options.chromeSelector;
const url = getStoryUrl(kind, story);
const tab = await launchNewTab(tabOptions);
let screenshot;
try {
await withTimeout(options.chromeLoadTimeout)(tab.loadUrl(url));
screenshot = await tab.captureScreenshot(selector);
await fs.outputFile(outputPath, screenshot);
} catch (err) {
if (err instanceof TimeoutError) {
debug(`Timed out waiting for "${url}" to load`);
} else {
throw err;
}
} finally {
await tab.close();
}
return screenshot;
}
const fs = require('fs-extra');
const osnap = require('osnap/src/ios');
const { withRetries } = require('@loki/core');
const { createWebsocketTarget } = require('@loki/target-native-core');
const saveScreenshotToFile = withRetries(3)(async filename => {
await osnap.saveToFile({ filename });
const { size } = await fs.stat(filename);
if (size === 0) {
throw new Error('Screenshot failed ');
}
});
const createIOSSimulatorTarget = socketUri =>
createWebsocketTarget(socketUri, 'ios', saveScreenshotToFile);
module.exports = createIOSSimulatorTarget;
};
const getPositionInViewport = async selector => {
try {
return await executeFunctionWithWindow(getSelectorBoxSize, selector);
} catch (error) {
if (error.message === 'No visible elements found') {
throw new Error(
`Unable to get position of selector "${selector}". Review the \`chromeSelector\` option and make sure your story doesn't crash.`
);
}
throw error;
}
};
client.captureScreenshot = withRetries(options.chromeRetries)(
withTimeout(CAPTURING_SCREENSHOT_TIMEOUT, 'captureScreenshot')(
async (selector = 'body') => {
debug(`Getting viewport position of "${selector}"`);
const position = await getPositionInViewport(selector);
if (position.width === 0 || position.height === 0) {
throw new Error(
`Selector "${selector} has zero width or height. Can't capture screenshot.`
);
}
const clip = {
scale: 1,
x: Math.floor(position.x),
y: Math.floor(position.y),
width: Math.ceil(position.width),