Skip to content

Commit

Permalink
Migrate server-side http_resources service to packages (#142537)
Browse files Browse the repository at this point in the history
Co-authored-by: pgayvallet <[email protected]>
Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
3 people authored Oct 4, 2022
1 parent 5ff2232 commit 1c8e0ed
Show file tree
Hide file tree
Showing 43 changed files with 675 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,9 @@ packages/core/http/core-http-context-server-internal @elastic/kibana-core
packages/core/http/core-http-context-server-mocks @elastic/kibana-core
packages/core/http/core-http-request-handler-context-server @elastic/kibana-core
packages/core/http/core-http-request-handler-context-server-internal @elastic/kibana-core
packages/core/http/core-http-resources-server @elastic/kibana-core
packages/core/http/core-http-resources-server-internal @elastic/kibana-core
packages/core/http/core-http-resources-server-mocks @elastic/kibana-core
packages/core/http/core-http-router-server-internal @elastic/kibana-core
packages/core/http/core-http-router-server-mocks @elastic/kibana-core
packages/core/http/core-http-server @elastic/kibana-core
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@
"@kbn/core-http-context-server-mocks": "link:bazel-bin/packages/core/http/core-http-context-server-mocks",
"@kbn/core-http-request-handler-context-server": "link:bazel-bin/packages/core/http/core-http-request-handler-context-server",
"@kbn/core-http-request-handler-context-server-internal": "link:bazel-bin/packages/core/http/core-http-request-handler-context-server-internal",
"@kbn/core-http-resources-server": "link:bazel-bin/packages/core/http/core-http-resources-server",
"@kbn/core-http-resources-server-internal": "link:bazel-bin/packages/core/http/core-http-resources-server-internal",
"@kbn/core-http-resources-server-mocks": "link:bazel-bin/packages/core/http/core-http-resources-server-mocks",
"@kbn/core-http-router-server-internal": "link:bazel-bin/packages/core/http/core-http-router-server-internal",
"@kbn/core-http-router-server-mocks": "link:bazel-bin/packages/core/http/core-http-router-server-mocks",
"@kbn/core-http-server": "link:bazel-bin/packages/core/http/core-http-server",
Expand Down Expand Up @@ -942,6 +945,9 @@
"@types/kbn__core-http-context-server-mocks": "link:bazel-bin/packages/core/http/core-http-context-server-mocks/npm_module_types",
"@types/kbn__core-http-request-handler-context-server": "link:bazel-bin/packages/core/http/core-http-request-handler-context-server/npm_module_types",
"@types/kbn__core-http-request-handler-context-server-internal": "link:bazel-bin/packages/core/http/core-http-request-handler-context-server-internal/npm_module_types",
"@types/kbn__core-http-resources-server": "link:bazel-bin/packages/core/http/core-http-resources-server/npm_module_types",
"@types/kbn__core-http-resources-server-internal": "link:bazel-bin/packages/core/http/core-http-resources-server-internal/npm_module_types",
"@types/kbn__core-http-resources-server-mocks": "link:bazel-bin/packages/core/http/core-http-resources-server-mocks/npm_module_types",
"@types/kbn__core-http-router-server-internal": "link:bazel-bin/packages/core/http/core-http-router-server-internal/npm_module_types",
"@types/kbn__core-http-router-server-mocks": "link:bazel-bin/packages/core/http/core-http-router-server-mocks/npm_module_types",
"@types/kbn__core-http-server": "link:bazel-bin/packages/core/http/core-http-server/npm_module_types",
Expand Down
6 changes: 6 additions & 0 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ filegroup(
"//packages/core/http/core-http-context-server-mocks:build",
"//packages/core/http/core-http-request-handler-context-server:build",
"//packages/core/http/core-http-request-handler-context-server-internal:build",
"//packages/core/http/core-http-resources-server:build",
"//packages/core/http/core-http-resources-server-internal:build",
"//packages/core/http/core-http-resources-server-mocks:build",
"//packages/core/http/core-http-router-server-internal:build",
"//packages/core/http/core-http-router-server-mocks:build",
"//packages/core/http/core-http-server:build",
Expand Down Expand Up @@ -418,6 +421,9 @@ filegroup(
"//packages/core/http/core-http-context-server-mocks:build_types",
"//packages/core/http/core-http-request-handler-context-server:build_types",
"//packages/core/http/core-http-request-handler-context-server-internal:build_types",
"//packages/core/http/core-http-resources-server:build_types",
"//packages/core/http/core-http-resources-server-internal:build_types",
"//packages/core/http/core-http-resources-server-mocks:build_types",
"//packages/core/http/core-http-router-server-internal:build_types",
"//packages/core/http/core-http-router-server-mocks:build_types",
"//packages/core/http/core-http-server:build_types",
Expand Down
115 changes: 115 additions & 0 deletions packages/core/http/core-http-resources-server-internal/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
load("@npm//@bazel/typescript:index.bzl", "ts_config")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm", "pkg_npm_types", "ts_project")

PKG_DIRNAME = "core-http-resources-server-internal"
PKG_REQUIRE_NAME = "@kbn/core-http-resources-server-internal"

SOURCE_FILES = glob(
[
"**/*.ts",
],
exclude = [
"**/*.config.js",
"**/*.mock.*",
"**/*.test.*",
"**/*.stories.*",
"**/__snapshots__/**",
"**/integration_tests/**",
"**/mocks/**",
"**/scripts/**",
"**/storybook/**",
"**/test_fixtures/**",
"**/test_helpers/**",
],
)

SRCS = SOURCE_FILES

filegroup(
name = "srcs",
srcs = SRCS,
)

NPM_MODULE_EXTRA_FILES = [
"package.json",
]

RUNTIME_DEPS = [
"@npm//elastic-apm-node",
"//packages/kbn-logging",
"//packages/kbn-apm-config-loader",
]

TYPES_DEPS = [
"@npm//@types/node",
"@npm//@types/jest",
"@npm//elastic-apm-node",
"//packages/kbn-apm-config-loader:npm_module_types",
"//packages/kbn-logging:npm_module_types",
"//packages/core/http/core-http-server:npm_module_types",
"//packages/core/http/core-http-resources-server:npm_module_types",
"//packages/core/rendering/core-rendering-server-internal:npm_module_types",
"//packages/core/http/core-http-request-handler-context-server:npm_module_types",
]

jsts_transpiler(
name = "target_node",
srcs = SRCS,
build_pkg_name = package_name(),
)

ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
"//:tsconfig.base.json",
"//:tsconfig.bazel.json",
],
)

ts_project(
name = "tsc_types",
args = ['--pretty'],
srcs = SRCS,
deps = TYPES_DEPS,
declaration = True,
declaration_map = True,
emit_declaration_only = True,
out_dir = "target_types",
tsconfig = ":tsconfig",
)

js_library(
name = PKG_DIRNAME,
srcs = NPM_MODULE_EXTRA_FILES,
deps = RUNTIME_DEPS + [":target_node"],
package_name = PKG_REQUIRE_NAME,
visibility = ["//visibility:public"],
)

pkg_npm(
name = "npm_module",
deps = [":" + PKG_DIRNAME],
)

filegroup(
name = "build",
srcs = [":npm_module"],
visibility = ["//visibility:public"],
)

pkg_npm_types(
name = "npm_module_types",
srcs = SRCS,
deps = [":tsc_types"],
package_name = PKG_REQUIRE_NAME,
tsconfig = ":tsconfig",
visibility = ["//visibility:public"],
)

filegroup(
name = "build_types",
srcs = [":npm_module_types"],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/core-http-resources-server-internal

This package contains the internal types and implementation for Core's internal `http` resources service.
11 changes: 11 additions & 0 deletions packages/core/http/core-http-resources-server-internal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 { HttpResourcesService } from './src';

export type { InternalHttpResourcesPreboot, InternalHttpResourcesSetup } from './src';
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/core/http/core-http-resources-server-internal'],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "shared-common",
"id": "@kbn/core-http-resources-server-internal",
"owner": "@elastic/kibana-core",
"runtimeDeps": [],
"typeDeps": [],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@kbn/core-http-resources-server-internal",
"private": true,
"version": "1.0.0",
"main": "./target_node/index.js",
"license": "SSPL-1.0 OR Elastic License 2.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import type { RouteConfig } from '@kbn/core-http-server';

import { mockCoreContext } from '@kbn/core-base-server-mocks';
import { httpServiceMock, httpServerMock } from '@kbn/core-http-server-mocks';
import { coreMock } from '../mocks';
import { renderingServiceMock } from '@kbn/core-rendering-server-mocks';
import { HttpResourcesService, PrebootDeps, SetupDeps } from './http_resources_service';
import { httpResourcesMock } from './http_resources_service.mock';
import { HttpResources } from '..';
import type { HttpResources } from '@kbn/core-http-resources-server';
import {
createCoreRequestHandlerContextMock,
createHttpResourcesResponseFactory,
} from './test_helpers/http_resources_service_test_mocks';

const coreContext = mockCoreContext.create();

Expand All @@ -26,7 +28,7 @@ describe('HttpResources service', () => {
let setupDeps: SetupDeps;
let router: ReturnType<typeof httpServiceMock.createRouter>;
const kibanaRequest = httpServerMock.createKibanaRequest();
const context = coreMock.createCustomRequestHandlerContext({});
const context = createCoreRequestHandlerContextMock();
const apmConfig = { mockApmConfig: true };

beforeEach(() => {
Expand Down Expand Up @@ -66,7 +68,7 @@ describe('HttpResources service', () => {
});
const [[, routeHandler]] = router.get.mock.calls;

const responseFactory = httpResourcesMock.createResponseFactory();
const responseFactory = createHttpResourcesResponseFactory();
await routeHandler(context, kibanaRequest, responseFactory);
expect(getDeps().rendering.render).toHaveBeenCalledWith(
kibanaRequest,
Expand All @@ -92,7 +94,7 @@ describe('HttpResources service', () => {

const [[, routeHandler]] = router.get.mock.calls;

const responseFactory = httpResourcesMock.createResponseFactory();
const responseFactory = createHttpResourcesResponseFactory();
await routeHandler(context, kibanaRequest, responseFactory);

expect(responseFactory.ok).toHaveBeenCalledWith({
Expand All @@ -112,7 +114,7 @@ describe('HttpResources service', () => {
});
const [[, routeHandler]] = router.get.mock.calls;

const responseFactory = httpResourcesMock.createResponseFactory();
const responseFactory = createHttpResourcesResponseFactory();
await routeHandler(context, kibanaRequest, responseFactory);
expect(getDeps().rendering.render).toHaveBeenCalledWith(
kibanaRequest,
Expand All @@ -138,7 +140,7 @@ describe('HttpResources service', () => {

const [[, routeHandler]] = router.get.mock.calls;

const responseFactory = httpResourcesMock.createResponseFactory();
const responseFactory = createHttpResourcesResponseFactory();
await routeHandler(context, kibanaRequest, responseFactory);

expect(responseFactory.ok).toHaveBeenCalledWith({
Expand All @@ -159,7 +161,7 @@ describe('HttpResources service', () => {
});
const [[, routeHandler]] = router.get.mock.calls;

const responseFactory = httpResourcesMock.createResponseFactory();
const responseFactory = createHttpResourcesResponseFactory();
await routeHandler(context, kibanaRequest, responseFactory);
expect(responseFactory.ok).toHaveBeenCalledWith({
body: htmlBody,
Expand All @@ -186,7 +188,7 @@ describe('HttpResources service', () => {

const [[, routeHandler]] = router.get.mock.calls;

const responseFactory = httpResourcesMock.createResponseFactory();
const responseFactory = createHttpResourcesResponseFactory();
await routeHandler(context, kibanaRequest, responseFactory);

expect(responseFactory.ok).toHaveBeenCalledWith({
Expand All @@ -208,7 +210,7 @@ describe('HttpResources service', () => {
});
const [[, routeHandler]] = router.get.mock.calls;

const responseFactory = httpResourcesMock.createResponseFactory();
const responseFactory = createHttpResourcesResponseFactory();
await routeHandler(context, kibanaRequest, responseFactory);
expect(responseFactory.ok).toHaveBeenCalledWith({
body: jsBody,
Expand All @@ -235,7 +237,7 @@ describe('HttpResources service', () => {

const [[, routeHandler]] = router.get.mock.calls;

const responseFactory = httpResourcesMock.createResponseFactory();
const responseFactory = createHttpResourcesResponseFactory();
await routeHandler(context, kibanaRequest, responseFactory);

expect(responseFactory.ok).toHaveBeenCalledWith({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,29 @@ import type {
InternalRenderingServiceSetup,
} from '@kbn/core-rendering-server-internal';
import type { RequestHandlerContext } from '@kbn/core-http-request-handler-context-server';
import {
InternalHttpResourcesSetup,
import type {
HttpResources,
HttpResourcesResponseOptions,
HttpResourcesRenderOptions,
HttpResourcesRequestHandler,
HttpResourcesServiceToolkit,
} from './types';
} from '@kbn/core-http-resources-server';

import type { InternalHttpResourcesSetup } from './types';

import { getApmConfig } from './get_apm_config';

/**
* @internal
*/
export interface PrebootDeps {
http: InternalHttpServicePreboot;
rendering: InternalRenderingServicePreboot;
}

/**
* @internal
*/
export interface SetupDeps {
http: InternalHttpServiceSetup;
rendering: InternalRenderingServiceSetup;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 { HttpResourcesService } from './http_resources_service';

export type { InternalHttpResourcesPreboot, InternalHttpResourcesSetup } from './types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 type { HttpResourcesServiceToolkit } from '@kbn/core-http-resources-server';
import { httpServerMock } from '@kbn/core-http-server-mocks';
import { uiSettingsServiceMock } from '@kbn/core-ui-settings-server-mocks';

// partial duplicate of coreMock
export function createCoreRequestHandlerContextMock() {
return {
core: {
uiSettings: { client: uiSettingsServiceMock.createClient() },
},
};
}

// duplicate of public mock for internal testing only
export function createHttpResourcesResponseFactory() {
const mocked: jest.Mocked<HttpResourcesServiceToolkit> = {
renderCoreApp: jest.fn(),
renderAnonymousCoreApp: jest.fn(),
renderHtml: jest.fn(),
renderJs: jest.fn(),
};

return {
...httpServerMock.createResponseFactory(),
...mocked,
};
}
Loading

0 comments on commit 1c8e0ed

Please sign in to comment.