Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.14] [Screenshotting] Server package for stateless code (#188390) #193232

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,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-connectors": "link:packages/kbn-search-connectors",
"@kbn/search-connectors-plugin": "link:x-pack/plugins/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;
};
18 changes: 18 additions & 0 deletions packages/kbn-screenshotting-server/src/paths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 { ChromiumArchivePaths } from './paths';

describe('ChromiumArchivePaths', () => {
it('should download chromium archive to a specific path', () => {
const ChromiumArchivePathsInstance = new ChromiumArchivePaths();
expect(ChromiumArchivePathsInstance.archivesPath).toEqual(
expect.stringContaining('kibana/.chromium')
);
});
});
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 Expand Up @@ -101,7 +102,7 @@ export class ChromiumArchivePaths {
];

// zip files get downloaded to a .chromium directory in the kibana root
public readonly archivesPath = path.resolve(__dirname, '../../../../../../.chromium');
public readonly archivesPath = path.resolve(__dirname, '../../../.chromium');

public find(platform: string, architecture: string, packages: PackageInfo[] = this.packages) {
return packages.find((p) => p.platform === platform && p.architecture === architecture);
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 @@ -1372,6 +1372,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-connectors": ["packages/kbn-search-connectors"],
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
Loading