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

[CB-INTERNAL] move nested renderer to promises #1273

Merged
merged 5 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
186 changes: 64 additions & 122 deletions src/registry/domain/nested-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,145 +1,87 @@
import async from 'async';
import _ from 'lodash';

import settings from '../../resources/settings';
import strings from '../../resources';
import { Config } from '../../types';
import {
GetComponentResult,
RendererOptions
} from '../routes/helpers/get-component';

type Cb = (err: string | null, data: string) => void;
type Options = {
interface Options {
ip?: string;
version?: string;
name?: string;
headers?: Record<string, string>;
parameters?: Record<string, string>;
};
type Params = {
components: Options[];
options: Options;
callback: Cb;
};

const sanitise = {
componentParams(component: string, options: Options | Cb, callback?: Cb) {
return {
...sanitise.options(options, callback),
componentName: component
};
},
componentsParams(
components: Options[],
options: Options | Cb,
callback: Cb
): Params {
return {
...sanitise.options(options, callback),
components: components
};
},
headers(h = {}) {
return {
...h,
accept: settings.registry.acceptRenderedHeader
};
},
options(
options: Options | Cb,
callback?: Cb
): { options: Options; callback: Cb } {
const cb = !callback && typeof options === 'function' ? options : callback;
const opts = typeof options === 'function' ? {} : options;

return { callback: cb!, options: opts };
}
};

const validate = {
callback(c: Cb) {
if (!c || typeof c !== 'function') {
throw new Error(
strings.errors.registry.NESTED_RENDERER_CALLBACK_IS_NOT_VALID
);
}
},
componentParams(params: { componentName: string; callback: Cb }) {
if (!params.componentName) {
throw new Error(
strings.errors.registry.NESTED_RENDERER_COMPONENT_NAME_IS_NOT_VALID
);
}

validate.callback(params.callback);
},
componentsParams(params: Params) {
if (_.isEmpty(params.components)) {
throw new Error(
strings.errors.registry.NESTED_RENDERER_COMPONENTS_IS_NOT_VALID
);
}
}

validate.callback(params.callback);
}
};
export default function nestedRenderer(
rendererWithCallback: (
options: RendererOptions,
cb: (result: GetComponentResult) => void
) => void,
conf: Config
) {
const renderer = (options: RendererOptions) =>
new Promise<string>((res, rej) => {
rendererWithCallback(options, result => {
if (result.response.error) {
rej(result.response.error);
} else {
res(result.response.html!);
}
});
});

export default function nestedRenderer(renderer: any, conf: Config) {
return {
renderComponent(
componentName: string,
renderOptions: Options | Cb,
callback?: Cb
) {
const p = sanitise.componentParams(
componentName,
renderOptions,
callback
);
validate.componentParams(p);
options: Options = {}
): Promise<string> {
if (!componentName) {
throw new Error(
strings.errors.registry.NESTED_RENDERER_COMPONENT_NAME_IS_NOT_VALID
);
}

return renderer(
{
conf: conf,
headers: sanitise.headers(p.options.headers),
name: componentName,
parameters: p.options.parameters || {},
version: p.options.version || ''
return renderer({
conf: conf,
ip: options.ip || '',
headers: {
...options.headers,
accept: settings.registry.acceptRenderedHeader
},
(result: any) => {
if (result.response.error) {
return p.callback(result.response.error, undefined as any);
} else {
return p.callback(null, result.response.html);
}
}
);
name: componentName,
parameters: options.parameters || {},
version: options.version || ''
});
},
renderComponents(
components: Options[],
renderOptions: Options,
callback: Cb
) {
const p = sanitise.componentsParams(components, renderOptions, callback);
validate.componentsParams(p);
options: Options = {}
): Promise<Array<string | Error>> {
if (!components || !components.length) {
throw new Error(
strings.errors.registry.NESTED_RENDERER_COMPONENTS_IS_NOT_VALID
);
}

async.map(
p.components,
(component, cb) => {
renderer(
{
conf: conf,
headers: sanitise.headers(p.options.headers),
name: component.name,
parameters: {
...p.options.parameters,
...component.parameters
},
version: component.version || ''
return Promise.all(
components.map(component => {
return renderer({
conf: conf,
headers: {
...options.headers,
accept: settings.registry.acceptRenderedHeader
},
(result: any) => {
const error = result.response.error;
cb(null, error ? new Error(error) : result.response.html);
}
);
},
p.callback as any
ip: component.ip || '',
name: component.name!,
parameters: {
...options.parameters,
...component.parameters
},
version: component.version || ''
}).catch(err => new Error(err));
})
);
}
};
Expand Down
13 changes: 7 additions & 6 deletions src/registry/routes/helpers/get-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Config, Repository } from '../../../types';
import { IncomingHttpHeaders } from 'http';
import { fromPromise } from 'universalify';

interface Options {
export interface RendererOptions {
conf: Config;
headers: IncomingHttpHeaders;
ip: string;
Expand All @@ -32,14 +32,15 @@ interface Options {
omitHref?: boolean;
}

export type GetComponentResult = {
export interface GetComponentResult {
status: number;
headers?: Record<string, string>;
response: {
type?: string;
code?: string;
error?: unknown;
version?: string;
html?: string;
requestVersion?: string;
name?: string;
details?: {
Expand All @@ -50,7 +51,7 @@ export type GetComponentResult = {
missingPlugins?: string[];
missingDependencies?: string[];
};
};
}

export default function getComponent(conf: Config, repository: Repository) {
const client = Client({ templates: conf.templates });
Expand All @@ -60,7 +61,7 @@ export default function getComponent(conf: Config, repository: Repository) {
});

const renderer = function (
options: Options,
options: RendererOptions,
cb: (result: GetComponentResult) => void
) {
const nestedRenderer = NestedRenderer(renderer, options.conf);
Expand Down Expand Up @@ -420,8 +421,8 @@ export default function getComponent(conf: Config, repository: Repository) {
env: conf.env,
params,
plugins: conf.plugins,
renderComponent: nestedRenderer.renderComponent,
renderComponents: nestedRenderer.renderComponents,
renderComponent: fromPromise(nestedRenderer.renderComponent),
renderComponents: fromPromise(nestedRenderer.renderComponents),
requestHeaders: options.headers,
requestIp: options.ip,
setEmptyResponse,
Expand Down
Loading