Skip to content

Commit

Permalink
refactor(core-api): discontinue the PluginAspect enum #885
Browse files Browse the repository at this point in the history
Also fixed a few typos here and there, linter warnings, etc.

Fixes #885

Signed-off-by: Peter Somogyvari <[email protected]>
  • Loading branch information
petermetz committed May 25, 2021
1 parent ee8f323 commit 23788d4
Show file tree
Hide file tree
Showing 20 changed files with 72 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
ICactusPlugin,
IPluginWebService,
IWebServiceEndpoint,
PluginAspect,
} from "@hyperledger/cactus-core-api";

import { PluginRegistry } from "@hyperledger/cactus-core";
Expand Down Expand Up @@ -143,10 +142,6 @@ export class CarbonAccountingPlugin
return "@hyperledger/cactus-example-carbon-accounting-backend";
}

public getAspect(): PluginAspect {
return PluginAspect.BUSINESS_LOGIC;
}

public async enrollAdminV1(
req: EnrollAdminV1Request,
): Promise<EnrollAdminV1Response> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
ICactusPlugin,
IPluginWebService,
IWebServiceEndpoint,
PluginAspect,
} from "@hyperledger/cactus-core-api";
import {
DefaultApi as QuorumApi,
Expand Down Expand Up @@ -160,8 +159,4 @@ export class SupplyChainCactusPlugin
public getPackageName(): string {
return "@hyperledger/cactus-example-supply-chain-backend";
}

public getAspect(): PluginAspect {
return PluginAspect.BUSINESS_LOGIC;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
IPluginLedgerConnector,
IWebServiceEndpoint,
IPluginWebService,
PluginAspect,
ICactusPlugin,
ICactusPluginOptions,
} from "@hyperledger/cactus-core-api";
Expand Down Expand Up @@ -122,10 +121,6 @@ export class PluginLedgerConnectorStub
return `@hyperledger/cactus-plugin-ledger-connector-stub`;
}

public getAspect(): PluginAspect {
return PluginAspect.LEDGER_CONNECTOR;
}

public async getConsensusAlgorithmFamily(): Promise<
ConsensusAlgorithmFamily
> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { PluginAspect } from "./plugin-aspect";

/**
* The common interface definiton that plugin classes can use to inherit from
* The common interface definition that plugin classes can use to inherit from
* when defining their own options interface for their constructors.
*
* Not all plugins need to have a unique plugin ID, so if the plugin you are
Expand Down Expand Up @@ -57,7 +55,7 @@ export interface ICactusPlugin {
* Returns the NodeJS/npm package name of the plugin which is used to identify
* plugin instances at runtime and differentiate them from other types of plugins.
*
* Important: This is not just uniqely identifying the plugin aspect, but the
* Important: This is not just uniquely identifying the plugin aspect, but the
* implementation as well.
* For example a plugin aspect would we `ledger-connector` or `storage` and
* implementations are the ones within those
Expand All @@ -70,17 +68,12 @@ export interface ICactusPlugin {
* Hyperledger Besu deployment)
*/
getPackageName(): string;

/**
* Returns the aspect of which this plugin implementation belongs to such as the aspect of `ledger-connector` or
* `storage` for example.
* There can be any number of plugin implementations for each aspect.
*/
getAspect(): PluginAspect;
}

export function isICactusPlugin(
pluginInstance: any,
): pluginInstance is ICactusPlugin {
return typeof pluginInstance?.getPackageName === "function";
export function isICactusPlugin(x: unknown): x is ICactusPlugin {
return (
!!x &&
typeof (x as ICactusPlugin).getPackageName === "function" &&
typeof (x as ICactusPlugin).getInstanceId === "function"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IPluginKeychain } from "./i-plugin-keychain";

export function isIPluginKeychain(x: unknown): x is IPluginKeychain {
return (
!!x &&
typeof (x as IPluginKeychain).delete === "function" &&
typeof (x as IPluginKeychain).get === "function" &&
typeof (x as IPluginKeychain).getInstanceId === "function" &&
typeof (x as IPluginKeychain).getKeychainId === "function" &&
typeof (x as IPluginKeychain).getPackageName === "function" &&
typeof (x as IPluginKeychain).has === "function" &&
typeof (x as IPluginKeychain).set === "function"
);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,14 @@ export interface IPluginWebService extends ICactusPlugin {
shutdown(): Promise<void>;
}

export function isIPluginWebService(
pluginInstance: unknown,
): pluginInstance is IPluginWebService {
export function isIPluginWebService(x: unknown): x is IPluginWebService {
return (
!!pluginInstance &&
typeof (pluginInstance as IPluginWebService).registerWebServices ===
"function" &&
typeof (pluginInstance as IPluginWebService).getOrCreateWebServices ===
"function" &&
typeof (pluginInstance as IPluginWebService).getHttpServer === "function" &&
typeof (pluginInstance as IPluginWebService).getPackageName ===
"function" &&
typeof (pluginInstance as IPluginWebService).getAspect === "function" &&
typeof (pluginInstance as IPluginWebService).shutdown === "function"
!!x &&
typeof (x as IPluginWebService).registerWebServices === "function" &&
typeof (x as IPluginWebService).getOrCreateWebServices === "function" &&
typeof (x as IPluginWebService).getHttpServer === "function" &&
typeof (x as IPluginWebService).getPackageName === "function" &&
typeof (x as IPluginWebService).getInstanceId === "function" &&
typeof (x as IPluginWebService).shutdown === "function"
);
}
3 changes: 1 addition & 2 deletions packages/cactus-core-api/src/main/typescript/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./generated/openapi/typescript-axios/base";
export { IPluginLedgerConnector } from "./plugin/ledger-connector/i-plugin-ledger-connector";
export { IPluginConsortium } from "./plugin/consortium/i-plugin-consortium";
export { IPluginKeychain } from "./plugin/keychain/i-plugin-keychain";
export { isIPluginKeychain } from "./plugin/keychain/is-i-plugin-keychain";
export { IExpressRequestHandler } from "./plugin/web-service/i-express-request-handler";

export {
Expand All @@ -20,8 +21,6 @@ export {
isICactusPlugin,
} from "./plugin/i-cactus-plugin";

export { PluginAspect } from "./plugin/plugin-aspect";

export { IPluginFactoryOptions } from "./i-plugin-factory-options";

export { PluginFactoryFactory } from "./plugin-factory-factory";
Expand Down
29 changes: 4 additions & 25 deletions packages/cactus-core/src/main/typescript/plugin-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ICactusPlugin,
IPluginKeychain,
isICactusPlugin,
PluginAspect,
isIPluginKeychain,
} from "@hyperledger/cactus-core-api";

/**
Expand Down Expand Up @@ -71,12 +71,6 @@ export class PluginRegistry {
) as T;
}

public getOneByAspect<T extends ICactusPlugin>(aspect: PluginAspect): T {
return this.findOneByAspect(aspect).orElseThrow(
() => new Error(`No plugin with aspect: ${aspect}`),
) as T;
}

public findOneByPackageName<T extends ICactusPlugin>(
packageName: string,
): Optional<T> {
Expand All @@ -94,36 +88,21 @@ export class PluginRegistry {
) as T[];
}

public findOneByAspect<T extends ICactusPlugin>(
aspect: PluginAspect,
): Optional<T> {
const plugin = this.getPlugins().find((p) => p.getAspect() === aspect);
return Optional.ofNullable(plugin as T);
}

public findOneByKeychainId<T extends IPluginKeychain>(keychainId: string): T {
const fnTag = "PluginRegistry#findOneByKeychainId()";
if (typeof keychainId !== "string" || keychainId.trim().length < 1) {
throw new Error(`${fnTag} need keychainId arg as non-blank string.`);
}

const plugin = this.findManyByAspect<IPluginKeychain>(
PluginAspect.KEYCHAIN,
).find((keychainPlugin) => keychainPlugin.getKeychainId() === keychainId);
const plugin = this.plugins
.filter((p) => isIPluginKeychain(p))
.find((p) => (p as IPluginKeychain).getKeychainId() === keychainId);

return Optional.ofNullable(plugin as T).orElseThrow(
() => new Error(`${fnTag} No keychain found for ID ${keychainId}`),
);
}

public findManyByAspect<T extends ICactusPlugin>(aspect: PluginAspect): T[] {
return this.getPlugins().filter((p) => p.getAspect() === aspect) as T[];
}

public hasByAspect(aspect: PluginAspect): boolean {
return this.findOneByAspect(aspect).isPresent();
}

public hasByPackageName(packageName: string): boolean {
return this.findOneByPackageName(packageName).isPresent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,42 @@ import { v4 as uuidv4 } from "uuid";

import { PluginRegistry } from "../../../main/typescript/public-api";

import {
ICactusPlugin,
IPluginKeychain,
PluginAspect,
} from "@hyperledger/cactus-core-api";
import { ICactusPlugin, IPluginKeychain } from "@hyperledger/cactus-core-api";

test("PluginRegistry", (tMain: Test) => {
test("findOneByKeychainId() finds plugin by keychain ID", (t: Test) => {
const keychainId = uuidv4();
const instanceId = uuidv4();

const mockKeychainPlugin = {
const mockKeychainPlugin: IPluginKeychain = {
getInstanceId: () => instanceId,
getKeychainId: () => keychainId,
getAspect: () => PluginAspect.KEYCHAIN,
} as IPluginKeychain;
delete: async () => {
throw new Error("This is a mock. Not implemented.");
},
has: async () => {
throw new Error("This is a mock. Not implemented.");
},
get: async () => {
throw new Error("This is a mock. Not implemented.");
},
set: async () => {
throw new Error("This is a mock. Not implemented.");
},
getPackageName: () => "@hyperledger/cactus-plugin-keychain-mock",
};

const pluginRegistry = new PluginRegistry({
plugins: [
mockKeychainPlugin,
{
getAspect: () => PluginAspect.CONSORTIUM,
getInstanceId: () => "some-mock-plugin-instance-id-1",
} as ICactusPlugin,
{
getAspect: () => PluginAspect.KV_STORAGE,
getInstanceId: () => "some-mock-plugin-instance-id-2",
} as ICactusPlugin,
{
getAspect: () => PluginAspect.LEDGER_CONNECTOR,
getInstanceId: () => "some-mock-plugin-instance-id-3",
} as ICactusPlugin,
],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { v4 as uuidv4 } from "uuid";
import {
ConsortiumDatabase,
IPluginWebService,
PluginAspect,
IWebServiceEndpoint,
ICactusPlugin,
ICactusPluginOptions,
Expand Down Expand Up @@ -224,10 +223,6 @@ export class PluginConsortiumManual
return `@hyperledger/cactus-plugin-consortium-manual`;
}

public getAspect(): PluginAspect {
return PluginAspect.CONSORTIUM;
}

public async getNodeJws(): Promise<JWSGeneral> {
const { keyPairPem, consortiumRepo: repo } = this.options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from "@hyperledger/cactus-common";
import {
ICactusPluginOptions,
PluginAspect,
IWebServiceEndpoint,
} from "@hyperledger/cactus-core-api";

Expand All @@ -20,15 +19,15 @@ import {

export interface IPluginKeychainMemoryOptions extends ICactusPluginOptions {
logLevel?: LogLevelDesc;
backend?: Map<string, any>;
backend?: Map<string, unknown>;
keychainId: string;
prometheusExporter?: PrometheusExporter;
}

export class PluginKeychainMemory {
public static readonly CLASS_NAME = "PluginKeychainMemory";

private readonly backend: Map<string, any>;
private readonly backend: Map<string, unknown>;
private readonly log: Logger;
private readonly instanceId: string;
public prometheusExporter: PrometheusExporter;
Expand Down Expand Up @@ -113,12 +112,8 @@ export class PluginKeychainMemory {
return `@hyperledger/cactus-plugin-keychain-memory`;
}

public getAspect(): PluginAspect {
return PluginAspect.KEYCHAIN;
}

async get<T>(key: string): Promise<T> {
return this.backend.get(key);
return this.backend.get(key) as T;
}

async has(key: string): Promise<boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
ICactusPluginOptions,
IPluginWebService,
IWebServiceEndpoint,
PluginAspect,
} from "@hyperledger/cactus-core-api";

import { DefaultApi } from "./generated/openapi/typescript-axios";
Expand All @@ -28,7 +27,7 @@ export interface IPluginKeychainVaultRemoteAdapterOptions
}

/**
* Class responsible for ecapsulating an API client object and then acting as
* Class responsible for encapsulating an API client object and then acting as
* an adapter (ta-da) between said API client object and the calling code to
* which it is (should be) transparent whether it is talking to an in-process
* plugin instance of the keychain plugin or an adapter backed by an API client
Expand Down Expand Up @@ -123,14 +122,17 @@ export class PluginKeychainVaultRemoteAdapter
public async get<T>(key: string): Promise<T> {
const { data } = await this.backend.getKeychainEntry({ key });
// FIXME what to do here? Does it make any sense to have the get() method
// of the keychain be generically parameterizable when we know we can only
// of the keychain be generically parameterized when we know we can only
// return a string anyway?
return data.value as any;
return (data.value as unknown) as T;
}

public async set<T>(key: string, value: T): Promise<void> {
// FIXME Does it make any sense to have the set() method be generic?
await this.backend.setKeychainEntry({ key, value: value as any });
await this.backend.setKeychainEntry({
key,
value: (value as unknown) as string,
});
}

public async delete(key: string): Promise<void> {
Expand All @@ -147,8 +149,4 @@ export class PluginKeychainVaultRemoteAdapter
public getPackageName(): string {
return `@hyperledger/cactus-plugin-keychain-vault`;
}

public getAspect(): PluginAspect {
return PluginAspect.KEYCHAIN;
}
}
Loading

0 comments on commit 23788d4

Please sign in to comment.