Skip to content

Commit

Permalink
Add experimental rawServerBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
kondi committed Dec 21, 2019
1 parent c5cfe9f commit 61c57c9
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/experimental/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { ServiceDefinition } from 'grpc';
import { Observable, Subscribable } from 'rxjs';

import {
addServerBuildMethods,
ClientFactoryConstructor,
createService,
createServiceClient,
DynamicMethods,
GenericServerBuilder,
getServiceNames,
grpcLoad,
GrpcService,
Expand All @@ -15,7 +18,8 @@ import {
} from '../utils';

type LooseReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type RequestType<T> = T extends (arg: infer A, ...args: any[]) => any ? A : never;
type FirstArgument<T> = T extends (arg: infer A, ...args: any[]) => any ? A : never;
type RequestType<T> = FirstArgument<T>;
type ResponseType<T> = LooseReturnType<T> extends Subscribable<infer A> ? A : never;

export type SerializedMessage<T> = Buffer & {
Expand Down Expand Up @@ -54,6 +58,12 @@ export type RawClientFactory<ClientFactory> = {
[GetterName in keyof ClientFactory]: () => RawService<LooseReturnType<ClientFactory[GetterName]>>
};

export type RawServerBuilder<ServerBuilder> = {
[AdderName in keyof ServerBuilder]: (
service: RawService<FirstArgument<ServerBuilder[AdderName]>>,
) => RawServerBuilder<ServerBuilder>
};

export function rawClientFactory<ClientFactory>(protoPath: string, packageName: string) {
class Constructor {
readonly __args: [string, grpc.ChannelCredentials, any | undefined];
Expand Down Expand Up @@ -86,6 +96,33 @@ export function rawClientFactory<ClientFactory>(protoPath: string, packageName:
return (Constructor as any) as ClientFactoryConstructor<RawClientFactory<ClientFactory>>;
}

export function rawServerBuilder<T>(
protoPath: string,
packageName: string,
server = new grpc.Server(),
): RawServerBuilder<T> & GenericServerBuilder<RawServerBuilder<T>> {
const adders: DynamicMethods = {};
const pkg = lookupPackage(grpcLoad(protoPath), packageName);
for (const name of getServiceNames(pkg)) {
adders[`add${name}`] = function(rxImpl: DynamicMethods) {
const serviceData = (pkg[name] as any) as GrpcService<any>;
const definition = serviceData.service;
typedKeys(definition).forEach(methodKey => {
const methodDefinition = definition[methodKey];
methodDefinition.requestDeserialize = (serialized: SerializedMessage<any>) => {
return serialized;
};
methodDefinition.responseSerialize = (alreadySerialized: SerializedMessage<any>) => {
return alreadySerialized;
};
});
server.addService(serviceData.service, createService(serviceData, rxImpl));
return this;
};
}
return addServerBuildMethods(adders, server) as any;
}

export function buildCodecsFactory<ClientFactory>(
protoPath: string,
packageName: string,
Expand Down

0 comments on commit 61c57c9

Please sign in to comment.