Skip to content

Commit

Permalink
Yet another declaration + tests (w/o grpc-js-xds)
Browse files Browse the repository at this point in the history
  • Loading branch information
enderteszla committed Nov 19, 2024
1 parent 4289fc4 commit f64867e
Show file tree
Hide file tree
Showing 43 changed files with 418 additions and 109 deletions.
4 changes: 3 additions & 1 deletion packages/grpc-health-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"scripts": {
"compile": "tsc -p .",
"prepare": "npm run generate-types && npm run compile",
"generate-types": "proto-loader-gen-types --keepCase --longs String --enums String --defaults --oneofs --includeComments --includeDirs proto/ -O src/generated health/v1/health.proto",
"test": "gulp test",
"pretest": "npm run generate-types && npm run generate-test-types && npm run compile",
"generate-types": "proto-loader-gen-types --keepCase --longs String --enums String --defaults --oneofs --includeComments --includeDirs proto/ -O src/generated --grpcLib=@grpc/grpc-js health/v1/health.proto",
"generate-test-types": "proto-loader-gen-types --keepCase --longs String --enums String --defaults --oneofs --includeComments --includeDirs proto/ -O test/generated --grpcLib=@grpc/grpc-js health/v1/health.proto"
},
"dependencies": {
Expand Down
121 changes: 120 additions & 1 deletion packages/grpc-health-check/src/generated/grpc/health/v1/Health.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,129 @@
// Original file: proto/health/v1/health.proto

import type * as grpc from '@grpc/grpc-js'
import type { MethodDefinition } from '@grpc/proto-loader'
import type { HealthCheckRequest as _grpc_health_v1_HealthCheckRequest, HealthCheckRequest__Output as _grpc_health_v1_HealthCheckRequest__Output } from '../../../grpc/health/v1/HealthCheckRequest';
import type { HealthCheckResponse as _grpc_health_v1_HealthCheckResponse, HealthCheckResponse__Output as _grpc_health_v1_HealthCheckResponse__Output } from '../../../grpc/health/v1/HealthCheckResponse';

export interface HealthDefinition {
/**
* Health is gRPC's mechanism for checking whether a server is able to handle
* RPCs. Its semantics are documented in
* https://github.com/grpc/grpc/blob/master/doc/health-checking.md.
*/
export interface HealthClient extends grpc.ServiceClient {
/**
* Check gets the health of the specified service. If the requested service
* is unknown, the call will fail with status NOT_FOUND. If the caller does
* not specify a service name, the server should respond with its overall
* health status.
*
* Clients should set a deadline when calling Check, and can declare the
* server unhealthy if they do not receive a timely response.
*
* Check implementations should be idempotent and side effect free.
*/
Check(argument: _grpc_health_v1_HealthCheckRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_grpc_health_v1_HealthCheckResponse__Output>): grpc.ClientUnaryCall;
Check(argument: _grpc_health_v1_HealthCheckRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_grpc_health_v1_HealthCheckResponse__Output>): grpc.ClientUnaryCall;
Check(argument: _grpc_health_v1_HealthCheckRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_grpc_health_v1_HealthCheckResponse__Output>): grpc.ClientUnaryCall;
Check(argument: _grpc_health_v1_HealthCheckRequest, callback: grpc.requestCallback<_grpc_health_v1_HealthCheckResponse__Output>): grpc.ClientUnaryCall;
/**
* Check gets the health of the specified service. If the requested service
* is unknown, the call will fail with status NOT_FOUND. If the caller does
* not specify a service name, the server should respond with its overall
* health status.
*
* Clients should set a deadline when calling Check, and can declare the
* server unhealthy if they do not receive a timely response.
*
* Check implementations should be idempotent and side effect free.
*/
check(argument: _grpc_health_v1_HealthCheckRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_grpc_health_v1_HealthCheckResponse__Output>): grpc.ClientUnaryCall;
check(argument: _grpc_health_v1_HealthCheckRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_grpc_health_v1_HealthCheckResponse__Output>): grpc.ClientUnaryCall;
check(argument: _grpc_health_v1_HealthCheckRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_grpc_health_v1_HealthCheckResponse__Output>): grpc.ClientUnaryCall;
check(argument: _grpc_health_v1_HealthCheckRequest, callback: grpc.requestCallback<_grpc_health_v1_HealthCheckResponse__Output>): grpc.ClientUnaryCall;

/**
* Performs a watch for the serving status of the requested service.
* The server will immediately send back a message indicating the current
* serving status. It will then subsequently send a new message whenever
* the service's serving status changes.
*
* If the requested service is unknown when the call is received, the
* server will send a message setting the serving status to
* SERVICE_UNKNOWN but will *not* terminate the call. If at some
* future point, the serving status of the service becomes known, the
* server will send a new message with the service's serving status.
*
* If the call terminates with status UNIMPLEMENTED, then clients
* should assume this method is not supported and should not retry the
* call. If the call terminates with any other status (including OK),
* clients should retry the call with appropriate exponential backoff.
*/
Watch(argument: _grpc_health_v1_HealthCheckRequest, metadata: grpc.Metadata, options?: grpc.CallOptions): grpc.ClientReadableStream<_grpc_health_v1_HealthCheckResponse__Output>;
Watch(argument: _grpc_health_v1_HealthCheckRequest, options?: grpc.CallOptions): grpc.ClientReadableStream<_grpc_health_v1_HealthCheckResponse__Output>;
/**
* Performs a watch for the serving status of the requested service.
* The server will immediately send back a message indicating the current
* serving status. It will then subsequently send a new message whenever
* the service's serving status changes.
*
* If the requested service is unknown when the call is received, the
* server will send a message setting the serving status to
* SERVICE_UNKNOWN but will *not* terminate the call. If at some
* future point, the serving status of the service becomes known, the
* server will send a new message with the service's serving status.
*
* If the call terminates with status UNIMPLEMENTED, then clients
* should assume this method is not supported and should not retry the
* call. If the call terminates with any other status (including OK),
* clients should retry the call with appropriate exponential backoff.
*/
watch(argument: _grpc_health_v1_HealthCheckRequest, metadata: grpc.Metadata, options?: grpc.CallOptions): grpc.ClientReadableStream<_grpc_health_v1_HealthCheckResponse__Output>;
watch(argument: _grpc_health_v1_HealthCheckRequest, options?: grpc.CallOptions): grpc.ClientReadableStream<_grpc_health_v1_HealthCheckResponse__Output>;

}

/**
* Health is gRPC's mechanism for checking whether a server is able to handle
* RPCs. Its semantics are documented in
* https://github.com/grpc/grpc/blob/master/doc/health-checking.md.
*/
export interface HealthHandlers extends grpc.UntypedServiceImplementation {
/**
* Check gets the health of the specified service. If the requested service
* is unknown, the call will fail with status NOT_FOUND. If the caller does
* not specify a service name, the server should respond with its overall
* health status.
*
* Clients should set a deadline when calling Check, and can declare the
* server unhealthy if they do not receive a timely response.
*
* Check implementations should be idempotent and side effect free.
*/
Check: grpc.handleUnaryCall<_grpc_health_v1_HealthCheckRequest__Output, _grpc_health_v1_HealthCheckResponse>;

/**
* Performs a watch for the serving status of the requested service.
* The server will immediately send back a message indicating the current
* serving status. It will then subsequently send a new message whenever
* the service's serving status changes.
*
* If the requested service is unknown when the call is received, the
* server will send a message setting the serving status to
* SERVICE_UNKNOWN but will *not* terminate the call. If at some
* future point, the serving status of the service becomes known, the
* server will send a new message with the service's serving status.
*
* If the call terminates with status UNIMPLEMENTED, then clients
* should assume this method is not supported and should not retry the
* call. If the call terminates with any other status (including OK),
* clients should retry the call with appropriate exponential backoff.
*/
Watch: grpc.handleServerStreamingCall<_grpc_health_v1_HealthCheckRequest__Output, _grpc_health_v1_HealthCheckResponse>;

}

export interface HealthDefinition extends grpc.ServiceDefinition {
Check: MethodDefinition<_grpc_health_v1_HealthCheckRequest, _grpc_health_v1_HealthCheckResponse, _grpc_health_v1_HealthCheckRequest__Output, _grpc_health_v1_HealthCheckResponse__Output>
Watch: MethodDefinition<_grpc_health_v1_HealthCheckRequest, _grpc_health_v1_HealthCheckResponse, _grpc_health_v1_HealthCheckRequest__Output, _grpc_health_v1_HealthCheckResponse__Output>
}
26 changes: 26 additions & 0 deletions packages/grpc-health-check/src/generated/health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type * as grpc from '@grpc/grpc-js';
import type { MessageTypeDefinition } from '@grpc/proto-loader';

import type { HealthClient as _grpc_health_v1_HealthClient, HealthDefinition as _grpc_health_v1_HealthDefinition } from './grpc/health/v1/Health';

type SubtypeConstructor<Subtype extends grpc.ServiceClient> = {
new (address: string, credentials: grpc.ChannelCredentials, options?: Partial<grpc.ChannelOptions>): Subtype;
} & grpc.ServiceClientConstructor;

export interface ProtoGrpcType extends grpc.GrpcObject {
grpc: {
health: {
v1: {
/**
* Health is gRPC's mechanism for checking whether a server is able to handle
* RPCs. Its semantics are documented in
* https://github.com/grpc/grpc/blob/master/doc/health-checking.md.
*/
Health: SubtypeConstructor<_grpc_health_v1_HealthClient> & { service: _grpc_health_v1_HealthDefinition }
HealthCheckRequest: MessageTypeDefinition
HealthCheckResponse: MessageTypeDefinition
}
}
}
}

9 changes: 6 additions & 3 deletions packages/grpc-health-check/src/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@
*/

import * as path from 'path';
import { loadSync, ServiceDefinition } from '@grpc/proto-loader';
import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
import type { ProtoGrpcType as HealthGrpc } from './generated/health';
import { HealthCheckRequest__Output } from './generated/grpc/health/v1/HealthCheckRequest';
import { HealthCheckResponse } from './generated/grpc/health/v1/HealthCheckResponse';
import { sendUnaryData, Server, ServerUnaryCall, ServerWritableStream } from './server-type';

const loadedProto = loadSync('health/v1/health.proto', {
const healthProto = protoLoader.loadSync('health/v1/health.proto', {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs: [`${__dirname}/../../proto`],
});
const healthGrpc = grpc.loadPackageDefinition(healthProto) as HealthGrpc;

export const service = loadedProto['grpc.health.v1.Health'] as ServiceDefinition;
export const service = healthGrpc.grpc.health.v1.Health.service;

const GRPC_STATUS_NOT_FOUND = 5;

Expand Down
6 changes: 4 additions & 2 deletions packages/grpc-health-check/src/server-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*
*/

import { ServiceDefinition } from '@grpc/proto-loader';
import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
import { ObjectReadable, ObjectWritable } from './object-stream';
import { EventEmitter } from 'events';

Expand Down Expand Up @@ -99,5 +100,6 @@ export interface UntypedServiceImplementation {
}

export interface Server {
addService(service: ServiceDefinition, implementation: UntypedServiceImplementation): void;
addService(service: protoLoader.ServiceDefinition, implementation: UntypedServiceImplementation): void;
addService<ImplType = UntypedServiceImplementation>(service: grpc.ServiceDefinition, implementation: ImplType): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { HealthCheckResponse as _grpc_health_v1_HealthCheckResponse, Health
* RPCs. Its semantics are documented in
* https://github.com/grpc/grpc/blob/master/doc/health-checking.md.
*/
export interface HealthClient extends grpc.Client {
export interface HealthClient extends grpc.ServiceClient {
/**
* Check gets the health of the specified service. If the requested service
* is unknown, the call will fail with status NOT_FOUND. If the caller does
Expand Down
10 changes: 5 additions & 5 deletions packages/grpc-health-check/test/generated/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import type { MessageTypeDefinition } from '@grpc/proto-loader';

import type { HealthClient as _grpc_health_v1_HealthClient, HealthDefinition as _grpc_health_v1_HealthDefinition } from './grpc/health/v1/Health';

type SubtypeConstructor<Constructor extends new (...args: any) => any, Subtype> = {
new(...args: ConstructorParameters<Constructor>): Subtype;
};
type SubtypeConstructor<Subtype extends grpc.ServiceClient> = {
new (address: string, credentials: grpc.ChannelCredentials, options?: Partial<grpc.ChannelOptions>): Subtype;
} & grpc.ServiceClientConstructor;

export interface ProtoGrpcType {
export interface ProtoGrpcType extends grpc.GrpcObject {
grpc: {
health: {
v1: {
Expand All @@ -16,7 +16,7 @@ export interface ProtoGrpcType {
* RPCs. Its semantics are documented in
* https://github.com/grpc/grpc/blob/master/doc/health-checking.md.
*/
Health: SubtypeConstructor<typeof grpc.Client, _grpc_health_v1_HealthClient> & { service: _grpc_health_v1_HealthDefinition }
Health: SubtypeConstructor<_grpc_health_v1_HealthClient> & { service: _grpc_health_v1_HealthDefinition }
HealthCheckRequest: MessageTypeDefinition
HealthCheckResponse: MessageTypeDefinition
}
Expand Down
Loading

0 comments on commit f64867e

Please sign in to comment.