Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}