Skip to content

Commit

Permalink
WIP yet another declaration + tests refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
enderteszla committed Nov 19, 2024
1 parent 8f0104c commit 131015b
Show file tree
Hide file tree
Showing 41 changed files with 262 additions and 110 deletions.
7 changes: 6 additions & 1 deletion packages/grpc-health-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
"scripts": {
"compile": "tsc -p .",
"prepare": "npm run generate-types && npm run compile",
"test": "gulp test",
"check": "npm run lint",
"fix": "eslint --fix src/*.ts test/*.ts",
"pretest": "npm run generate-types && npm run generate-test-types && npm run compile",
"posttest": "npm run check && madge -c ./build/src",
"generate-types": "proto-loader-gen-types --keepCase --longs String --enums String --defaults --oneofs --includeComments --includeDirs proto/ -O src/generated 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": {
"@grpc/proto-loader": "^0.7.13"
"@grpc/proto-loader": "file:../proto-loader"
},
"files": [
"LICENSE",
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 '../../../../index'
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 '../index';
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
}
}
}
}

5 changes: 4 additions & 1 deletion packages/grpc-health-check/src/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@
*/

import * as path from 'path';
import * as grpc from '@grpc/grpc-js';
import { loadSync, ServiceDefinition } 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 = 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;

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
15 changes: 13 additions & 2 deletions packages/grpc-health-check/test/test-health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import * as assert from 'assert';
import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
import { HealthImplementation, ServingStatusMap, service as healthServiceDefinition } from '../src/health';
import { HealthClient } from './generated/grpc/health/v1/Health';
import type { ProtoGrpcType as HealthGrpc } from './generated/health';
import type { HealthClient } from './generated/grpc/health/v1/Health';
import { HealthCheckResponse__Output, _grpc_health_v1_HealthCheckResponse_ServingStatus__Output } from './generated/grpc/health/v1/HealthCheckResponse';

describe('Health checking', () => {
Expand All @@ -28,6 +30,15 @@ describe('Health checking', () => {
'grpc.test.TestServiceNotServing': 'NOT_SERVING',
'grpc.test.TestServiceServing': 'SERVING'
};
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;
let healthServer: grpc.Server;
let healthClient: HealthClient;
let healthImpl: HealthImplementation;
Expand All @@ -41,7 +52,7 @@ describe('Health checking', () => {
return;
}
const HealthClientConstructor = grpc.makeClientConstructor(healthServiceDefinition, 'grpc.health.v1.HealthService');
healthClient = new HealthClientConstructor(`localhost:${port}`, grpc.credentials.createInsecure()) as unknown as HealthClient;
healthClient = new HealthClientConstructor(`localhost:${port}`, grpc.credentials.createInsecure());
healthServer.start();
done();
});
Expand Down
11 changes: 3 additions & 8 deletions packages/grpc-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,11 @@
"format": "clang-format -i -style=\"{Language: JavaScript, BasedOnStyle: Google, ColumnLimit: 80}\" src/*.ts test/*.ts",
"lint": "eslint src/*.ts test/*.ts",
"prepare": "npm run generate-types && npm run compile",
"test": "gulp test",
"check": "npm run lint",
"fix": "eslint --fix src/*.ts test/*.ts",
"pretest": "npm run generate-types && npm run generate-test-types && npm run compile",
"posttest": "npm run check && madge -c ./build/src",
"generate-types": "proto-loader-gen-types --keepCase --longs String --enums String --defaults --oneofs --includeComments --includeDirs proto/ --include-dirs test/fixtures/ -O src/generated/ --grpcLib ../index channelz.proto",
"generate-test-types": "proto-loader-gen-types --keepCase --longs String --enums String --defaults --oneofs --includeComments --include-dirs test/fixtures/ -O test/generated/ --grpcLib ../../src/index test_service.proto"
"generate-types": "./node_modules/.bin/proto-loader-gen-types --keepCase --longs String --enums String --defaults --oneofs --includeComments --includeDirs proto/ --include-dirs test/fixtures/ -O src/generated/ --grpcLib ../index channelz.proto",
"generate-test-types": "./node_modules/.bin/proto-loader-gen-types --keepCase --longs String --enums String --defaults --oneofs --includeComments --include-dirs test/fixtures/ -O test/generated/ --grpcLib ../../src/index test_service.proto"
},
"dependencies": {
"@grpc/proto-loader": "^0.7.13",
"@grpc/proto-loader": "file:../proto-loader",
"@js-sdsl/ordered-map": "^4.4.2"
},
"files": [
Expand Down
7 changes: 2 additions & 5 deletions packages/grpc-js/src/channelz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,11 +881,8 @@ export function getChannelzServiceDefinition(): ChannelzDefinition {
oneofs: true,
includeDirs: [`${__dirname}/../../proto`],
});
const channelzGrpcObject = loadPackageDefinition(
loadedProto
) as unknown as ChannelzProtoGrpcType;
loadedChannelzDefinition =
channelzGrpcObject.grpc.channelz.v1.Channelz.service;
const channelzGrpcObject = loadPackageDefinition(loadedProto) as ChannelzProtoGrpcType;
loadedChannelzDefinition = channelzGrpcObject.grpc.channelz.v1.Channelz.service;
return loadedChannelzDefinition;
}

Expand Down
10 changes: 5 additions & 5 deletions packages/grpc-js/src/generated/channelz.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type * as grpc from '../index';
import type { MessageTypeDefinition } from '@grpc/proto-loader';

import type { ChannelzClient as _grpc_channelz_v1_ChannelzClient, ChannelzDefinition as _grpc_channelz_v1_ChannelzDefinition } from './grpc/channelz/v1/Channelz.ts';
import type { ChannelzClient as _grpc_channelz_v1_ChannelzClient, ChannelzDefinition as _grpc_channelz_v1_ChannelzDefinition } from './grpc/channelz/v1/Channelz';

type SubtypeConstructor<Constructor extends new (...args: any) => any, Subtype> = grpc.ServiceClientConstructor & {
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 extends grpc.GrpcObject {
google: {
Expand Down Expand Up @@ -38,7 +38,7 @@ export interface ProtoGrpcType extends grpc.GrpcObject {
* Channelz is a service exposed by gRPC servers that provides detailed debug
* information.
*/
Channelz: SubtypeConstructor<typeof grpc.Client, _grpc_channelz_v1_ChannelzClient> & { service: _grpc_channelz_v1_ChannelzDefinition }
Channelz: SubtypeConstructor<_grpc_channelz_v1_ChannelzClient> & { service: _grpc_channelz_v1_ChannelzDefinition }
GetChannelRequest: MessageTypeDefinition
GetChannelResponse: MessageTypeDefinition
GetServerRequest: MessageTypeDefinition
Expand Down
2 changes: 1 addition & 1 deletion packages/grpc-js/src/generated/grpc/channelz/v1/Address.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Original file: proto/channelz.proto

import type { Any as _google_protobuf_Any, Any__Output as _google_protobuf_Any__Output } from '../../../google/protobuf/Any.ts';
import type { Any as _google_protobuf_Any, Any__Output as _google_protobuf_Any__Output } from '../../../google/protobuf/Any';

/**
* An address type not included above.
Expand Down
8 changes: 4 additions & 4 deletions packages/grpc-js/src/generated/grpc/channelz/v1/Channel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Original file: proto/channelz.proto

import type { ChannelRef as _grpc_channelz_v1_ChannelRef, ChannelRef__Output as _grpc_channelz_v1_ChannelRef__Output } from '../../../grpc/channelz/v1/ChannelRef.ts';
import type { ChannelData as _grpc_channelz_v1_ChannelData, ChannelData__Output as _grpc_channelz_v1_ChannelData__Output } from '../../../grpc/channelz/v1/ChannelData.ts';
import type { SubchannelRef as _grpc_channelz_v1_SubchannelRef, SubchannelRef__Output as _grpc_channelz_v1_SubchannelRef__Output } from '../../../grpc/channelz/v1/SubchannelRef.ts';
import type { SocketRef as _grpc_channelz_v1_SocketRef, SocketRef__Output as _grpc_channelz_v1_SocketRef__Output } from '../../../grpc/channelz/v1/SocketRef.ts';
import type { ChannelRef as _grpc_channelz_v1_ChannelRef, ChannelRef__Output as _grpc_channelz_v1_ChannelRef__Output } from '../../../grpc/channelz/v1/ChannelRef';
import type { ChannelData as _grpc_channelz_v1_ChannelData, ChannelData__Output as _grpc_channelz_v1_ChannelData__Output } from '../../../grpc/channelz/v1/ChannelData';
import type { SubchannelRef as _grpc_channelz_v1_SubchannelRef, SubchannelRef__Output as _grpc_channelz_v1_SubchannelRef__Output } from '../../../grpc/channelz/v1/SubchannelRef';
import type { SocketRef as _grpc_channelz_v1_SocketRef, SocketRef__Output as _grpc_channelz_v1_SocketRef__Output } from '../../../grpc/channelz/v1/SocketRef';

/**
* Channel is a logical grouping of channels, subchannels, and sockets.
Expand Down
Loading

0 comments on commit 131015b

Please sign in to comment.