Skip to content

Commit

Permalink
[Screenshotting] Server package for stateless code (elastic#188390)
Browse files Browse the repository at this point in the history
## Summary

This PR moves code that is usually impacted by a Puppeteer/Chromium
upgrade to a package. The same work is happening to a lesser degree in
the 7.17 branch. Together, these changes will ease the manual labor of
backporting the upgrades from main to 7.17.

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
tsullivan and kibanamachine authored Aug 22, 2024
1 parent 53b5d7c commit bf673d9
Show file tree
Hide file tree
Showing 47 changed files with 193 additions and 85 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ examples/screenshot_mode_example @elastic/appex-sharedux
src/plugins/screenshot_mode @elastic/appex-sharedux
x-pack/examples/screenshotting_example @elastic/appex-sharedux
x-pack/plugins/screenshotting @elastic/kibana-reporting-services
packages/kbn-screenshotting-server @elastic/appex-sharedux
packages/kbn-search-api-panels @elastic/search-kibana
x-pack/plugins/search_assistant @elastic/search-kibana
packages/kbn-search-connectors @elastic/search-kibana
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@
"@kbn/screenshot-mode-plugin": "link:src/plugins/screenshot_mode",
"@kbn/screenshotting-example-plugin": "link:x-pack/examples/screenshotting_example",
"@kbn/screenshotting-plugin": "link:x-pack/plugins/screenshotting",
"@kbn/screenshotting-server": "link:packages/kbn-screenshotting-server",
"@kbn/search-api-panels": "link:packages/kbn-search-api-panels",
"@kbn/search-assistant": "link:x-pack/plugins/search_assistant",
"@kbn/search-connectors": "link:packages/kbn-search-connectors",
Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-screenshotting-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/screenshotting-server

Stateless code pertaining to the capture of screenshots for Kibana Reporting
13 changes: 13 additions & 0 deletions packages/kbn-screenshotting-server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { args } from './src/args';
export { ChromiumArchivePaths, type PackageInfo } from './src/paths';
export { getChromiumPackage } from './src/get_chromium_package';
export { type ConfigType, createConfig, config, durationToNumber } from './src/config';
export { ConfigSchema } from './src/config/schema';
13 changes: 13 additions & 0 deletions packages/kbn-screenshotting-server/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-screenshotting-server'],
};
5 changes: 5 additions & 0 deletions packages/kbn-screenshotting-server/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "shared-server",
"id": "@kbn/screenshotting-server",
"owner": "@elastic/appex-sharedux"
}
6 changes: 6 additions & 0 deletions packages/kbn-screenshotting-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@kbn/screenshotting-server",
"private": true,
"version": "1.0.0",
"license": "SSPL-1.0 OR Elastic License 2.0"
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import os from 'os';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import os from 'os';
import type { ConfigType } from '../../../config';
import type { ConfigType } from './config';

interface WindowSize {
height: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { Logger } from '@kbn/core/server';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { set } from '@kbn/safer-lodash-set';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

jest.mock('getos', () => jest.fn());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import getOsSync from 'getos';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { PluginConfigDescriptor } from '@kbn/core/server';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { schema, TypeOf, offeringBasedSchema } from '@kbn/config-schema';
Expand Down
23 changes: 23 additions & 0 deletions packages/kbn-screenshotting-server/src/get_chromium_package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import os from 'os';
import { ChromiumArchivePaths } from './paths';

const paths = new ChromiumArchivePaths();

export const getChromiumPackage = () => {
const platform = process.platform;
const architecture = os.arch();

const chromiumPackageInfo = paths.find(process.platform, architecture);
if (!chromiumPackageInfo) {
throw new Error(`Unsupported platform: ${platform}-${architecture}`);
}
return chromiumPackageInfo;
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import path from 'path';
Expand Down
21 changes: 21 additions & 0 deletions packages/kbn-screenshotting-server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node"
]
},
"include": [
"**/*.ts"
],
"exclude": [
"target/**/*"
],
"kbn_references": [
"@kbn/core",
"@kbn/safer-lodash-set",
"@kbn/config-schema"
]
}
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,8 @@
"@kbn/screenshotting-example-plugin/*": ["x-pack/examples/screenshotting_example/*"],
"@kbn/screenshotting-plugin": ["x-pack/plugins/screenshotting"],
"@kbn/screenshotting-plugin/*": ["x-pack/plugins/screenshotting/*"],
"@kbn/screenshotting-server": ["packages/kbn-screenshotting-server"],
"@kbn/screenshotting-server/*": ["packages/kbn-screenshotting-server/*"],
"@kbn/search-api-panels": ["packages/kbn-search-api-panels"],
"@kbn/search-api-panels/*": ["packages/kbn-search-api-panels/*"],
"@kbn/search-assistant": ["x-pack/plugins/search_assistant"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import type { Logger } from '@kbn/logging';
import { ScreenshotModePluginSetup } from '@kbn/screenshot-mode-plugin/server';
import { ConfigType } from '@kbn/screenshotting-server';
import * as puppeteer from 'puppeteer';
import { Size } from '../../../common/layout';
import { ConfigType } from '../../config';
import { PreserveLayout } from '../../layouts/preserve_layout';
import { HeadlessChromiumDriver } from './driver';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import {
KBN_SCREENSHOT_MODE_HEADER,
ScreenshotModePluginSetup,
} from '@kbn/screenshot-mode-plugin/server';
import { ConfigType } from '@kbn/screenshotting-server';
import { truncate } from 'lodash';
import open from 'opn';
import { ElementHandle, Page, EvaluateFunc, HTTPResponse } from 'puppeteer';
import { ElementHandle, EvaluateFunc, HTTPResponse, Page } from 'puppeteer';
import { Subject } from 'rxjs';
import { parse as parseUrl } from 'url';
import { getDisallowedOutgoingUrlError } from '.';
import { ConfigType } from '../../config';
import { Layout } from '../../layouts';
import { getPrintLayoutSelectors } from '../../layouts/print_layout';
import { allowRequest } from '../network_policy';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import type { Logger } from '@kbn/core/server';
import { loggerMock } from '@kbn/logging-mocks';
import type { ScreenshotModePluginSetup } from '@kbn/screenshot-mode-plugin/server';
import { ConfigType } from '@kbn/screenshotting-server';
import * as puppeteer from 'puppeteer';
import * as Rx from 'rxjs';
import { mergeMap, take } from 'rxjs';
import { DEFAULT_VIEWPORT, HeadlessChromiumDriverFactory } from '.';
import { ConfigType } from '../../../config';

jest.mock('puppeteer');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@

import type { Logger } from '@kbn/core/server';
import type { ScreenshotModePluginSetup } from '@kbn/screenshot-mode-plugin/server';
import { ConfigType, args } from '@kbn/screenshotting-server';
import { getDataPath } from '@kbn/utils';
import { spawn } from 'child_process';
import del from 'del';
import fs from 'fs';
import { uniq } from 'lodash';
import path from 'path';
import puppeteer, { Browser, ConsoleMessage, Page, Viewport, PageEvents } from 'puppeteer';
import puppeteer, { Browser, ConsoleMessage, Page, PageEvents, Viewport } from 'puppeteer';
import { createInterface } from 'readline';
import * as Rx from 'rxjs';
import { catchError, concatMap, ignoreElements, mergeMap, map, reduce, takeUntil, tap } from 'rxjs';
import { PerformanceMetrics } from '../../../../common/types';
import { catchError, concatMap, ignoreElements, map, mergeMap, reduce, takeUntil, tap } from 'rxjs';
import { getChromiumDisconnectedError } from '..';
import { errors } from '../../../../common';
import { ConfigType } from '../../../config';
import { PerformanceMetrics } from '../../../../common/types';
import { safeChildProcess } from '../../safe_child_process';
import { HeadlessChromiumDriver } from '../driver';
import { args } from './args';
import { getMetrics } from './metrics';

interface CreatePageOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ export const getDisallowedOutgoingUrlError = (interceptedUrl: string) =>
export { HeadlessChromiumDriver } from './driver';
export type { Context } from './driver';
export { DEFAULT_VIEWPORT, HeadlessChromiumDriverFactory } from './driver_factory';
export { ChromiumArchivePaths } from './paths';
export type { PackageInfo } from './paths';
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

import { loggingSystemMock } from '@kbn/core-logging-server-mocks';
import { PackageInfo } from '@kbn/screenshotting-server';
import assert from 'assert';
import axios from 'axios';
import path from 'path';
import { PackageInfo } from '..';
import { paths as chromiumArchivePaths } from '../../../utils';
import { download } from '../../download';
import { install } from '../../install';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* 2.0.
*/

import path from 'path';
import mockFs from 'mock-fs';
import { ChromiumArchivePaths, PackageInfo } from '@kbn/screenshotting-server';
import { access, readdir } from 'fs/promises';
import { ChromiumArchivePaths, PackageInfo } from '../chromium';
import { fetch } from './fetch';
import { sha256 } from './checksum';
import mockFs from 'mock-fs';
import path from 'path';
import { download } from '.';
import { sha256 } from './checksum';
import { fetch } from './fetch';

jest.mock('./checksum');
jest.mock('./fetch');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* 2.0.
*/

import { access } from 'fs/promises';
import del from 'del';
import type { Logger } from '@kbn/core/server';
import type { ChromiumArchivePaths, PackageInfo } from '../chromium';
import type { ChromiumArchivePaths, PackageInfo } from '@kbn/screenshotting-server';
import del from 'del';
import { access } from 'fs/promises';
import { sha256 } from './checksum';
import { fetch } from './fetch';

Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/screenshotting/server/browsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export { install } from './install';
export type { Context } from './chromium';
export {
getChromiumDisconnectedError,
ChromiumArchivePaths,
DEFAULT_VIEWPORT,
HeadlessChromiumDriver,
HeadlessChromiumDriverFactory,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/screenshotting/server/browsers/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* 2.0.
*/

import type { Logger } from '@kbn/core/server';
import { ChromiumArchivePaths, PackageInfo } from '@kbn/screenshotting-server';
import del from 'del';
import path from 'path';
import type { Logger } from '@kbn/core/server';
import { ChromiumArchivePaths, PackageInfo } from './chromium';
import { download } from './download';
import { sha256 } from './download/checksum';
import { extract } from './extract';
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/screenshotting/server/config/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { ConfigSchema } from './schema';
import { ConfigSchema } from '@kbn/screenshotting-server';

describe('ConfigSchema', () => {
it(`should produce correct config for context {"dev": false,"dist": false}`, () => {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/screenshotting/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function plugin(pluginInitializerContext: PluginInitializerContext)
return new ScreenshottingPlugin(pluginInitializerContext);
}

export { config } from './config';
export { config } from '@kbn/screenshotting-server';
export type {
PdfScreenshotOptions,
PdfScreenshotResult,
Expand Down
Loading

0 comments on commit bf673d9

Please sign in to comment.