-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cactus-core): add ConnectRPC service interface and type guard
Define the types and type guard needed for the API server to be able to recognize plugins that have implemented a ConnectRPC interface for their operations. Also, these types will be used by the plugins themselves to mark the implementations as valid for ConnectRPC usage. ConnectRPC is very similar to gRPC but has some nice features in addition to it such as the HTTP 2 and HTTP 1.1 proxying through express and fastify HTTP server instances. For further details see this link: https://connectrpc.com/ Signed-off-by: Peter Somogyvari <[email protected]>
- Loading branch information
Showing
5 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/cactus-core-api/src/main/typescript/plugin/crpc-service/i-plugin-crpc-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { ServiceType } from "@bufbuild/protobuf"; | ||
import type { ServiceImpl } from "@connectrpc/connect"; | ||
import type { UniversalHandlerOptions } from "@connectrpc/connect/protocol"; | ||
|
||
/** | ||
* Implementers of this interface are responsible for providing a Crpc service | ||
* that can be dynamically registered at runtime by the API server. | ||
* | ||
* It describes what methods a class (plugin) needs to implement | ||
* in order to be able to operate as a Cacti Crpc service (e.g. expose its | ||
* functionality through Crpc not just HTTP or SocketIO) | ||
* | ||
* @see {IPluginWebService} | ||
* @see {IPluginGrpcService} | ||
* @see {ApiServer} | ||
*/ | ||
export interface IPluginCrpcService { | ||
/** | ||
* Used by the API server and/or automated test cases when hooking up this | ||
* plugin instance into a crpc Server object. | ||
* | ||
* The returned pair of service | ||
* definition and implementation objects are passed in to the `.addService()` | ||
* method of the `Server` object of the `@connectrpc/connect` library. | ||
* | ||
* @see {ServiceDefinition} | ||
* @see {ServiceType} | ||
* @see {ICrpcSvcRegistration} | ||
*/ | ||
createCrpcSvcRegistrations( | ||
opts: unknown, | ||
): Promise<Array<ICrpcSvcRegistration<ServiceType>>>; | ||
} | ||
|
||
/** | ||
* A wrapper object that contains both the the definition and the implementation | ||
* objects of a crpc service that a plugin can implement to expose it's functionality | ||
* over crpc. | ||
* | ||
* @see {IPluginGrpcService} | ||
* @see {IGrpcSvcDefAndImplPair} | ||
*/ | ||
export interface ICrpcSvcRegistration<T extends ServiceType> { | ||
readonly definition: T; | ||
readonly implementation: Partial<ServiceImpl<T>>; | ||
readonly serviceName: string; | ||
readonly options?: Partial<UniversalHandlerOptions>; | ||
} | ||
|
||
/** | ||
* Custom (user-defined) Typescript type-guard that verifies at runtime whether | ||
* `x` is an implementation of {IPluginCrpcService} or not. | ||
* | ||
* @param x Literally any object or value that you'd want to verify for having | ||
* the right method(s) implemented in-order to qualify as an implementer of the | ||
* {IPluginCrpcService} interface. | ||
* | ||
* The {ApiServer} uses this to filter the total list of plugins down to the ones | ||
* that have crpc services of their own and then hook those up at runtime. | ||
* | ||
* @returns `true` if `x` does implement {IPluginCrpcService} `false` otherwise. | ||
*/ | ||
export function isIPluginCrpcService(x: unknown): x is IPluginCrpcService { | ||
return ( | ||
!!x && | ||
typeof (x as IPluginCrpcService).createCrpcSvcRegistrations === "function" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters