diff --git a/libraries/hermes/src/index.ts b/libraries/hermes/src/index.ts index 26dc1ec1d..6475cfdea 100644 --- a/libraries/hermes/src/index.ts +++ b/libraries/hermes/src/index.ts @@ -341,4 +341,3 @@ export * from './interfaces'; export * from './types'; export * from './classes'; export * from './utils/GrpcConverter'; -export * from './utils/ProtoGenerator'; diff --git a/libraries/hermes/src/utils/ProtoGenerator.ts b/libraries/hermes/src/utils/ProtoGenerator.ts deleted file mode 100644 index 434b3e66f..000000000 --- a/libraries/hermes/src/utils/ProtoGenerator.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { - ConduitProxyObject, - ConduitRouteObject, - SocketProtoDescription, -} from '@conduitplatform/grpc-sdk'; - -type ProtoTemplate = { - request: string; - response: string; - template: string; -}; - -export class ProtoGenerator { - private static _instance: ProtoGenerator; - private constructor(private readonly protoTemplate: ProtoTemplate) {} - - static getInstance(template?: ProtoTemplate) { - if (ProtoGenerator._instance) return ProtoGenerator._instance; - if (!template) throw new Error('No proto file template provided!'); - ProtoGenerator._instance = new ProtoGenerator(template); - return ProtoGenerator._instance; - } - - generateProtoFile( - moduleName: string, - paths: (ConduitRouteObject | SocketProtoDescription | ConduitProxyObject)[], - ) { - const formattedModuleName = this.getFormattedModuleName(moduleName); - const protoFunctions = this.createProtoFunctions(paths); - let protoFile = this.protoTemplate.template.replace( - 'MODULE_FUNCTIONS', - protoFunctions, - ); - protoFile = protoFile.replace('MODULE_NAME', formattedModuleName); - return { protoFile, formattedModuleName }; - } - - private getFormattedModuleName(moduleName: string) { - return moduleName.replace('-', '_'); - } - - private createProtoFunctions( - paths: (ConduitRouteObject | SocketProtoDescription | ConduitProxyObject)[], - ) { - let protoFunctions = ''; - - paths.forEach(r => { - if (r.hasOwnProperty('events')) { - protoFunctions += this.createProtoFunctionsForSocket( - r as SocketProtoDescription, - protoFunctions, - ); - } else { - protoFunctions += this.createProtoFunctionForRoute( - r as ConduitRouteObject, - protoFunctions, - ); - } - }); - - return protoFunctions; - } - - private createProtoFunctionsForSocket( - path: SocketProtoDescription, - protoFunctions: string, - ) { - let newFunctions = ''; - const events = JSON.parse(path.events); - Object.keys(events).forEach(event => { - const newFunction = this.createGrpcFunctionName(events[event].grpcFunction); - - if (protoFunctions.indexOf(`rpc ${newFunction}(`) !== -1) { - return; - } - - newFunctions += `rpc ${newFunction}(SocketRequest) returns (SocketResponse);\n`; - }); - - return newFunctions; - } - - private createProtoFunctionForRoute(path: ConduitRouteObject, protoFunctions: string) { - const newFunction = this.createGrpcFunctionName(path.grpcFunction); - - if (protoFunctions.indexOf(`rpc ${newFunction}(`) !== -1) { - return ''; - } - return `rpc ${newFunction}(${this.protoTemplate.request}) returns (${this.protoTemplate.response});\n`; - } - - private createGrpcFunctionName(grpcFunction: string) { - return grpcFunction.charAt(0).toUpperCase() + grpcFunction.slice(1); - } -} diff --git a/modules/router/src/Router.ts b/modules/router/src/Router.ts index cdcc04d49..421f8e5fe 100644 --- a/modules/router/src/Router.ts +++ b/modules/router/src/Router.ts @@ -17,7 +17,6 @@ import { ConduitRoutingController, ConduitSocket, grpcToConduitRoute, - ProtoGenerator, ProxyRoute, ProxyRouteT, proxyToConduitRoute, @@ -27,7 +26,7 @@ import { import { isNaN } from 'lodash'; import AppConfigSchema, { Config } from './config'; import * as models from './models'; -import { protoTemplate, getSwaggerMetadata } from './hermes'; +import { getSwaggerMetadata } from './hermes'; import { runMigrations } from './migrations'; import SecurityModule from './security'; import { AdminHandlers } from './admin'; @@ -78,7 +77,6 @@ export default class ConduitDefaultRouter extends ManagedModule { this.database = this.grpcSdk.databaseProvider!; await this.registerSchemas(); await runMigrations(this.grpcSdk); - ProtoGenerator.getInstance(protoTemplate); this._internalRouter = new ConduitRoutingController( this.getHttpPort()!, this.getSocketPort()!, diff --git a/modules/router/src/hermes/index.ts b/modules/router/src/hermes/index.ts index 7bcae134e..ebb277c5a 100644 --- a/modules/router/src/hermes/index.ts +++ b/modules/router/src/hermes/index.ts @@ -1,2 +1 @@ -export { default as protoTemplate } from './protoTemplate'; export * from './swaggerMetadata'; diff --git a/modules/router/src/hermes/protoTemplate.ts b/modules/router/src/hermes/protoTemplate.ts deleted file mode 100644 index 941c0e763..000000000 --- a/modules/router/src/hermes/protoTemplate.ts +++ /dev/null @@ -1,65 +0,0 @@ -const protoRequest = 'RouterRequest'; -const protoResponse = 'RouterResponse'; -const protoTemplate = ` -syntax = "proto3"; -package MODULE_NAME.router; - -service Router { - MODULE_FUNCTIONS -} - -message RouterRequest { - string params = 1; - string path = 2; - string headers = 3; - string context = 4; - string cookies = 5; - string bodyParams = 6; - string urlParams = 7; - string queryParams = 8; -} - - -message RouterResponse { - string result = 1; - string redirect = 2; - repeated Cookie setCookies = 3; - repeated Cookie removeCookies = 4; -} - -message SocketRequest { - string event = 1; - string socketId = 2; - string params = 3; - string context = 4; -} - -message Cookie { - string name = 1; - optional string value = 2; - Options options = 3; -} - -message Options { - bool httpOnly = 1; - bool secure = 2; - bool signed = 3; - int32 maxAge = 4; - string path = 5; - string domain = 6; - string sameSite = 7; - -} -message SocketResponse { - string event = 1; - string data = 2; - repeated string receivers = 3; - repeated string rooms = 4; -} -`; - -export default { - request: protoRequest, - response: protoResponse, - template: protoTemplate, -}; diff --git a/packages/admin/src/hermes/index.ts b/packages/admin/src/hermes/index.ts index 7bcae134e..ebb277c5a 100644 --- a/packages/admin/src/hermes/index.ts +++ b/packages/admin/src/hermes/index.ts @@ -1,2 +1 @@ -export { default as protoTemplate } from './protoTemplate'; export * from './swaggerMetadata'; diff --git a/packages/admin/src/hermes/protoTemplate.ts b/packages/admin/src/hermes/protoTemplate.ts deleted file mode 100644 index d9ed1f83c..000000000 --- a/packages/admin/src/hermes/protoTemplate.ts +++ /dev/null @@ -1,65 +0,0 @@ -const protoRequest = 'AdminRequest'; -const protoResponse = 'AdminResponse'; -const protoTemplate = ` -syntax = "proto3"; -package MODULE_NAME.admin; - -service Admin { - MODULE_FUNCTIONS -} - -message AdminRequest { - string params = 1; - string path = 2; - string headers = 3; - string context = 4; - string cookies = 5; - string bodyParams = 6; - string urlParams = 7; - string queryParams = 8; -} - -message AdminResponse { - string result = 1; - string redirect = 2; - repeated Cookie setCookies = 3; - repeated Cookie removeCookies = 4; -} - - -message SocketRequest { - string event = 1; - string socketId = 2; - string params = 3; - string context = 4; -} - -message Cookie { - string name = 1; - optional string value = 2; - Options options = 3; -} - -message Options { - bool httpOnly = 1; - bool secure = 2; - bool signed = 3; - int32 maxAge = 4; - string path = 5; - string domain = 6; - string sameSite = 7; - -} -message SocketResponse { - string event = 1; - string data = 2; - repeated string receivers = 3; - repeated string rooms = 4; -} -`; - -export default { - request: protoRequest, - response: protoResponse, - template: protoTemplate, -}; diff --git a/packages/admin/src/index.ts b/packages/admin/src/index.ts index 1e966c5a4..d1e02ac16 100644 --- a/packages/admin/src/index.ts +++ b/packages/admin/src/index.ts @@ -24,7 +24,7 @@ import * as middleware from './middleware'; import * as adminRoutes from './routes'; import * as models from './models'; import { AdminMiddleware } from './models'; -import { getSwaggerMetadata, protoTemplate } from './hermes'; +import { getSwaggerMetadata } from './hermes'; import path from 'path'; import { ConduitMiddleware, @@ -33,7 +33,6 @@ import { ConduitRoutingController, ConduitSocket, grpcToConduitRoute, - ProtoGenerator, ProxyRoute, ProxyRouteT, proxyToConduitRoute, @@ -84,7 +83,6 @@ export default class AdminModule extends IConduitAdmin { ) { super(commons); this.grpcSdk = grpcSdk; - ProtoGenerator.getInstance(protoTemplate); this._router = new ConduitRoutingController( this.getHttpPort()!, this.getSocketPort()!,