-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add direct-channel again (as test file)
- Loading branch information
Showing
11 changed files
with
201 additions
and
14 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
Empty file.
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,10 @@ | ||
// This is the entry point for the protocol if used with grpc (within docker) | ||
import { ProtocolConfig } from "./types"; | ||
import { Validator } from "./index"; | ||
|
||
const config: Partial<ProtocolConfig> = { | ||
host: process.env.HOST || "locahost", | ||
port: parseInt(process.env.PORT || "50051"), | ||
useGrpc: true, | ||
}; | ||
new Validator(config).bootstrap(); |
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
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,8 @@ | ||
import * as grpc from "@grpc/grpc-js"; | ||
|
||
export interface ProtocolConfig { | ||
host: string; | ||
port: number; | ||
useGrpc: boolean; | ||
channelOverride: grpc.Channel | undefined; | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./interfaces"; | ||
export * from "./metrics"; | ||
export * from "./config"; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import * as grpc from "@grpc/grpc-js"; | ||
import { Metadata, ServiceError } from "@grpc/grpc-js"; | ||
import { ChannelRef } from "@grpc/grpc-js/build/src/channelz"; | ||
import { ServerSurfaceCall } from "@grpc/grpc-js/build/src/server-call"; | ||
import { Call, MessageContext } from "@grpc/grpc-js/build/src/call-interface"; | ||
|
||
import * as runtime from "../../src/proto/kyverdk/runtime/v1/runtime"; | ||
import * as Buffer from "buffer"; | ||
|
||
export class DirectCall implements Call { | ||
private listener: grpc.InterceptingListener; | ||
private method: any; | ||
private requestType: any; | ||
private responseType: any; | ||
|
||
constructor(method: any, requestType: any, responseType: any) { | ||
this.listener = { | ||
onReceiveMessage(_message: any) {}, | ||
onReceiveStatus(_status: grpc.StatusObject) {}, | ||
onReceiveMetadata(_metadata: Metadata) {}, | ||
}; | ||
this.method = method; | ||
this.requestType = requestType; | ||
this.responseType = responseType; | ||
} | ||
|
||
cancelWithStatus(_status: grpc.status, _details: string): void {} | ||
|
||
getCallNumber(): number { | ||
return 0; | ||
} | ||
|
||
getPeer(): string { | ||
return ""; | ||
} | ||
|
||
halfClose(): void {} | ||
|
||
sendMessageWithContext(_context: MessageContext, message: Buffer): void { | ||
const payload = this.requestType.decode(message); | ||
|
||
// calls the grpc method directly without using grpc | ||
this.method( | ||
{ request: payload }, | ||
(error: ServiceError | null, response: any) => { | ||
if (error) { | ||
this.listener.onReceiveStatus(error); | ||
return; | ||
} | ||
|
||
const value = this.responseType.create(response); | ||
const msg = Buffer.Buffer.from( | ||
this.responseType.encode(value).finish() | ||
); | ||
|
||
this.listener.onReceiveMessage(msg); | ||
this.listener.onReceiveStatus({ | ||
code: grpc.status.OK, | ||
details: "", | ||
metadata: new Metadata(), | ||
}); | ||
} | ||
); | ||
} | ||
|
||
setCredentials(_credentials: grpc.CallCredentials): void {} | ||
|
||
start(_metadata: Metadata, listener: grpc.InterceptingListener): void { | ||
this.listener = listener; | ||
} | ||
|
||
startRead(): void {} | ||
} | ||
|
||
/* | ||
* DirectChannel can be used to call grpc methods directly without using grpc. | ||
* This is used to run the runtime service in the same process as the client. | ||
* It emulates the legacy mode of the runtime service without grpc/docker. | ||
* | ||
* Requirements: | ||
* - The integration must be written in typescript as well | ||
* - The integration must provide the server method to the protocol | ||
* - The grpc/proto naming convention must be the following: | ||
* - Method name: <methodName> | ||
* - Request type: <MethodName>Request | ||
* - Response type: <MethodName>Response | ||
* | ||
* Example: | ||
* ```proto | ||
* rpc GetRuntimeName(GetRuntimeNameRequest) returns (GetRuntimeNameResponse); | ||
* ``` | ||
* ```typescript | ||
* getRuntimeName(request: GetRuntimeNameRequest): GetRuntimeNameResponse {} | ||
* ``` | ||
*/ | ||
// @ts-ignore | ||
export class DirectChannel implements grpc.Channel { | ||
private services: {}; | ||
|
||
constructor(services: {}) { | ||
this.services = services; | ||
} | ||
|
||
close(): void {} | ||
|
||
createCall( | ||
call: string, | ||
_deadline: grpc.Deadline, | ||
_host: string | null | undefined, | ||
_parentCall: ServerSurfaceCall | null, | ||
_propagateFlags: number | null | undefined | ||
): Call { | ||
// Extract the method name from the path | ||
// Example: /kyverdk.runtime.v1.RuntimeService/GetRuntimeName -> GetRuntimeName | ||
const callName = call.split("/")[2]; | ||
|
||
// Convert the method name to camelCase | ||
const camelCaseCallName = | ||
callName.charAt(0).toLowerCase() + callName.slice(1); | ||
|
||
// Get the request type from the runtime service | ||
// Example: GetRuntimeName -> GetRuntimeNameRequest | ||
// @ts-ignore | ||
const requestType: any = runtime[`${callName}Request`]; | ||
|
||
// Get the response type from the runtime service | ||
// Example: GetRuntimeName -> GetRuntimeNameResponse | ||
// @ts-ignore | ||
const responseType: any = runtime[`${callName}Response`]; | ||
|
||
// @ts-ignore | ||
const method = this.services[camelCaseCallName]; | ||
|
||
// console.debug( | ||
// `Call method ${method.name} ${callName}Request -> ${callName}Response` | ||
// ); | ||
|
||
return new DirectCall(method, requestType, responseType); | ||
} | ||
|
||
getChannelzRef(): ChannelRef { | ||
return { id: 0, kind: "channel", name: "" }; | ||
} | ||
|
||
getConnectivityState(_tryToConnect: boolean): grpc.connectivityState { | ||
return grpc.connectivityState.READY; | ||
} | ||
|
||
getTarget(): string { | ||
return ""; | ||
} | ||
|
||
watchConnectivityState( | ||
_currentState: grpc.connectivityState, | ||
_deadline: Date | number, | ||
_callback: (error?: Error) => void | ||
): void {} | ||
} |
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